Selection Sort Stability

Computer Science Level 3

When the sorting algorithm below is performed on these three lists, which list will have preserved the relative order of the two equal 2s (\(2^+\) before \(2^*\))?

  • Start at the first element x[0] and set this as the minimum: minIndex = 0, minValue = x[0]

  • Iterate through the rest of the list one-by-one, replacing the minimum value as needed:

if x[i] < minValue:
    minIndex = i
    minValue = x[i]
  • Swap the final minimum element with the first element:
    x[0], x[minIndex] = x[minIndex], x[0]

  • Move on to the next element x[1] and repeat the entire process until you've started at every element in the list.


Problem Loading...

Note Loading...

Set Loading...