The Selection Sort
BadSelectionSort(array, start, stop)
- for i = start; i < stop; i++
- for j = i+1; j <= stop; j++
- if array[i] > array[j]
- swap(array[i],array[j])
- return
- This sort works by repeatedly placing the smallest item in the first available position.
- Consider the following example
i j array
- - 8 3 5 6 2 4 7
0 1 3 8 5 6 2 4 7
2 3 8 5 6 2 4 7
3 3 8 5 6 2 4 7
4 2 8 5 6 3 4 7
5 2 8 5 6 3 4 7
6 2 8 5 6 3 4 7
1 2 2 5 8 6 3 4 7
3 2 5 8 6 3 4 7
4 2 3 8 6 5 4 7
5 2 3 8 6 5 4 7
6 2 3 8 6 5 4 7
2 3 2 3 6 8 5 4 7
4 2 3 5 8 6 4 7
5 2 3 4 8 6 5 7
6 2 3 4 8 6 5 7
3 4 2 3 4 6 8 5 7
5 2 3 4 5 8 6 7
6 2 3 4 5 8 6 7
4 5 2 3 4 5 6 8 7
6 2 3 4 5 6 8 7
5 6 2 3 4 5 6 7 8
- What should we consider when we are looking at the performance of
an algorithm?
- The total run time of the algorithm?
- The number of operations?
- Which operations?
- Comparisons?
- Swaps?
- Other operations?
- We look at three different things when we talk about timing.
- Best case
- Worst Case
- Average Case
- Analysis of BadSelectionSort
for now, we will consider lines 3-4 as our basic operation.
i will obtain the values start, start+1, start+2, ... stop-1
wlog assume 0, 1, 2, 3, ... n-1
When i is 0, j will go from 1 to stop
lines 3-4 will be executed n-1 times.
When i is 1, j will go from 2 to n
lines 3-4 will be executed n-2 times.
...
When i is n-1, j will go from n to n
lines 3-4 will be executed 1 time.
So lines 3-4 will be executed
n-1 + n-2 + n-3 + ... + 3 + 2 + 1
= n(n-1)/2
= 1/2n2-1/2 n
This is O(n2)
- Notice noting about the order of the array will change the number of times line 4 is executed.
- So if we are considering comparisons, Best = Worst = Average case.
- How about swaps?
- If the array is in order, we will perform no swaps in line 4
- O(1)
- If the array is in reverse order, we will perform a swap in line 4 every time.
- O(n2)
- Notice as we move elements around, we don't really gain order on the
remainder of the array, just at the beginning.
- If the array is in random order, we will only swap 1/2 the time.
- O(n2/2)= O(n2)
- Do we need to perform all of these swaps?
SelectionSort(array, start, stop)
- for i= start; i < stop-1 ; i++
- min = i
- for j = i+1; j < stop; j++
- if array[min] > array[j]
- min = j
- if i != min
- swap(array[i], array[min])
- return
- Notice this has the same performance for lines 4-5 as line 3-4 above
- But only one swap occurs per iteration of the loop in lines 1-7
- When I made this change in the driver program from the d-array, I had
about a 40% improvment in time.
Is this algorithm correct?
- We can argue after the first iteration of the outer loop the element in position 0 must be the smallest.
- We can further argue that each further iteration of the outer loop essentially performs the same action.
- Thus when we are finished, the array must be in order.
Why did we look at this sort?
- We see how a little change in logic can change the performance of the algorithm.
- We see that big-O notation isn't everything.
- We have a fairly easy sort to perform analysis on.
- Everyone studies insertion sort.