Selection Sort Stability

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 + 2^+ before 2 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:

1
2
3
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.

[ 2 + , 2 , 1 , 3 ] [2^+,2^*,1,3] [ 2 + , 3 , 2 , 1 ] [2^+,3,2^*,1] [ 2 + , 1 , 2 , 3 ] [2^+,1,2^*,3]

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

0 solutions

No explanations have been posted yet. Check back later!

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...