Repair two selection-sort loop errors

A student writes the following Python code to sort an array A of n distinct integers in ascending order, where n is at least 2. Python uses zero-based indexing, and range excludes its stop argument.


Python
min_index = 0
for i in range(n - 1):
    for j in range(i + 1, n - 1):
        if A[j] < A[min_index]:
            min_index = j
    if min_index != i:
        A[i], A[min_index] = A[min_index], A[i]


Complete both repairs so the program correctly sorts every permitted input. Move the initialization of min_index to the beginning of the outer-loop body and assign it . Change the inner range's stop argument from n - 1 to . Keep the remaining code unchanged.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Selection Sort Questions: