CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis
2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input sequence such that a 1 ’ ≤ a 2 ’ ≤ · · · ≤ a n ’ Internal Sort - The data to be sorted is all stored in RAM. External Sort - Data to be sorted does not fit in the RAM and therefore stored in an external storage device (e.g. HardDisk)
To insert 12, we need to make room for it by moving first 36 and then 24. Insertion Sort Similar to sorting a hand of playing cards
Insertion Sort 36 12
Insertion Sort
6 Insertion Sort input array left sub-array right sub-array at each iteration, the array is divided in two sub-arrays: sorted unsorted
Insertion Sort
Alg.: INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] % Insert A[ j ] into the sorted sequence A[1.. j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key Insertion sort – sorts the elements in place a8a8 a7a7 a6a6 a5a5 a4a4 a3a3 a2a2 a1a key
Operation count for Insertion Sort times n n-1 t j : # of times the while statement is executed at iteration j INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] % Insert A[ j ] into the sorted …. i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key cost c 1 c 2 0 c 4 c 5 c 6 c 7 c 8
Best Case Analysis The array is already sorted A[i] ≤ key upon the first time the while loop test is run then (t j = 1) and
Worst Case Analysis The array is sorted in reverse order ◦ Always A[i] > key in while loop test ◦ Have to compare key with all elements to the left of the j th position compare with j-1 elements t j = j
Bubble Sort Swaps adjacent elements that are out of order Repeatedly pass through the array Simpler, but slower than Insertion sort 123n i j
Bubble Sort Example i = 1j j j j j j j i = 2j i = 3j i = 4j i = 5j i = 6j i = 7 j
Bubble Sort Alg.: BUBBLESORT(A) for i 1 to length[A] do for j length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] i = 1j i
Bubble-Sort Running Time Alg.: BUBBLESORT(A) for i 1 to length[A] do for j length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] Comparisons and Exchanges: n 2 /2 c1 c1 c2 c2 c3 c3
Selection Sort Find the smallest element in the array and exchange it with the element in the first position Find the second smallest element and exchange it with the element in the second position Continue until the array is sorted Example
n 2 /2 comparisons Analysis of Selection Sort Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] cost times c 1 1 c 2 n c 3 n-1 c 4 c 5 c 6 n-1 n exchanges