























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























































































Divides the array into two parts: already sorted, and not yet sorted.
On each pass,
Straight Selection Sort
values [ 0 ]
[ 1 ]
Selection Sort: Pass One
values [ 0 ]
[ 1 ]
Selection Sort: Pass Two
values [ 0 ]
[ 1 ]
Selection Sort: End Pass Two
values [ 0 ]
[ 1 ]
U N S O R T E D
Selection Sort: End Pass Three
values [ 0 ]
[ 1 ]
Selection Sort: Pass Four
values [ 0 ]
[ 1 ]
values [ 0 ]
[ 1 ]
4 compares for values[0]
3 compares for values[1]
2 compares for values[2]
1 compare for values[3]
= 4 + 3 + 2 + 1
Arithmetic series:
template
for(int index = start + 1 ; index <= end ; index++) if (values[ index] < values [indexOfMin]) indexOfMin = index ;
return indexOfMin;
}
template
// Post: Sorts array values[0.. numValues-1 ] // into ascending order by key { int endIndex = numValues - 1; for (int current=0; current<endIndex; current++)
Swap (values[current], values[MinIndex(values, current, endIndex)]);
}
Compares neighboring pairs of array elements,
On each pass, this causes the smallest element to “bubble up” to its correct place in the array.
Bubble Sort
values [ 0 ]
[ 1 ]
Snapshot of BubbleSort