Ch6-Sorting (1 Slide Per Page)
Ch6-Sorting (1 Slide Per Page)
89 45 68 90 34 17
17 | 45 68 90 34 89
17 34 | 68 90 45 89
17 34 45 | 90 68 89
17 34 45 68 | 90 89
17 34 45 68 89 | 90
- Search the array for the smallest element and swap that element with the
first one.
- Search for the second smallest and swap that element with the second one.
- Repeat the described steps until the whole array is sorted.
6.1 Selection Sort
ALGORITHM selectionSort(A[0..n-1])
for i 0 to n-2 do
- Search the array for the smallest element and swap that element with the
first one.
- Search for the second smallest and swap that element with the second one.
- Repeat the described steps until the whole array is sorted.
6.1 Selection Sort
ALGORITHM selectionSort(A[0..n-1])
for i 0 to n-2 do main operation
mini i (find the smallest
for j i+1 to n-1 do element A[mini]
if A[j] < A[mini] mini j among
A[i],…,A[n-1])
tmp A[i]
(swap A[i] and
A[i] A[mini] A[mini])
A[mini] tmp
- Search the array for the smallest element and swap that element with the
first one.
- Search for the second smallest and swap that element with the second one.
- Repeat the described steps until the whole array is sorted.
i j A[0] A[1] A[2] A[3] A[4] A[5] mini
0 89 45 68 90 34 17 0
0 1 89 45 68 90 34 17 1
0 2 89 45 68 90 34 17 1 find the smallest element i=0:
0 3 89 45 68 90 34 17 1 among A[0], A[1], …, A[5] n-1 comparisons
0 4 89 45 68 90 34 17 4
0 5 89 45 68 90 34 17 5
0 5 17 45 68 90 34 89 5
1 17 45 68 90 34 89 1
1 2 17 45 68 90 34 89 1 find the smallest element
1 3 17 45 68 90 34 89 4 i=1:
among A[1],…, A[5] n-2 comparisons
1 4 17 45 68 90 34 89 4
1 5 17 45 68 90 34 89 4
1 5 17 34 68 90 45 89 4
2 17 34 68 90 45 89 2
2 3 17 34 68 90 45 89 2 find the smallest i=2:
2 4 17 34 68 90 45 89 4 among A[2],…, A[5] n-3 comparisons
2 5 17 34 68 90 45 89 4
2 5 17 34 45 90 68 89 4
3 17 34 45 90 68 89 3
3 4 17 34 45 90 68 89 4
3 5 17 34 45 90 68 89 4
3 5 17 34 45 68 90 89 4
4 17 34 45 68 90 89 4 find the smallest i=n-2:
4 5 17 34 45 68 90 89 5 among A[4], A[5] 1 comparison
4 5 17 34 45 68 89 90 5
Total: (n−1) + (n−2) + … + 2 + 1 = ½n(n-1) comparisons. The running time is O(n2).
6.1 Selection Sort: Proof of Correctness
ALGORITHM selectionSort(A[0..n-1])
for i 0 to n-2 do
mini i
for j i+1 to n-1 do
if A[j] < A[mini] mini j
swap A[i] and A[mini]
▪ The array at any moment is divided into two parts: sorted and unsorted, and
any element of the sorted sub-array is less than or equal to any element of
the unsorted one.
▪ In each pass of the algorithm, the minimum element of the unsorted sub-
array is found and swapped with the first element of the unsorted sub-array.
The sorted sub-array is thus extended by one element in every pass.
6.1 Selection Sort: Proof of Correctness
ALGORITHM selectionSort(A[0..n-1])
for i 0 to n-2 do
mini i
for j i+1 to n-1 do
if A[j] < A[mini] mini j
swap A[i] and A[mini]
Loop invariant
1 23 78 45 8 32 56
2 23 78 45 8 32 56
3 23 45 78 8 32 56
4 8 23 45 78 32 56
5 8 23 32 45 78 56
6 8 23 32 45 56 78
The array at any moment is divided into two parts: sorted and unsorted.
In each pass of the algorithm, the first element of the unsorted sub-list
is inserted into the sorted sub-list at the appropriate place.
6.3 Insertion Sort
ALGORITHM insertionSort(A[0..n-1])
//The algorithm sorts a given array A
//Input: an array A[0..n-1] of n numbers
//Output: array A[0..n-1] in ascending order
for i 0 to n-1 do main operation
tmp A[i]
j i-1
while (j≥0 and tmp < A[j]) do
A[j+1] A[j]
j j-1
A[j+1] tmp
Analysis: CW1
Testing the Sorting Algorithms
10,000 5.17 sec. 3.18 sec. 10.88 s. 0.05 sec. 0.05 sec.
20,000 22.03 sec 13.51 sec. 45.09 s. 0.11 sec 0.05 sec.
40,000 1 min. 31.01 sec. 53.66 sec 2 min. 59.40 s. 0.22 sec. 0.11 sec.
80,000 6 min. 06.30 sec. 3 min. 46.62 sec. 12 min. 27.15 s. 0.49 sec. 0.28 sec.
iterative recursive