Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.

Similar presentations


Presentation on theme: "CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis."— Presentation transcript:

1 CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

2 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)

3 To insert 12, we need to make room for it by moving first 36 and then 24. Insertion Sort 6 10 24 12 36 Similar to sorting a hand of playing cards

4 6 10 24 Insertion Sort 36 12

5 Insertion Sort 6 10 24 36 12

6 6 Insertion Sort 5 2 4 6 1 3 input array left sub-array right sub-array at each iteration, the array is divided in two sub-arrays: sorted unsorted

7 Insertion Sort

8 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 a1a1 12345678 key

9 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

10 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

11 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

12 Bubble Sort Swaps adjacent elements that are out of order Repeatedly pass through the array Simpler, but slower than Insertion sort 123n i 1329648 j

13 Bubble Sort Example 1329648 i = 1j 3129648 j 3219648 j 3291648 j 3296148 j 3296418 j 3296481 j 3296481 i = 2j 3964821 i = 3j 9648321 i = 4j 9684321 i = 5j 9864321 i = 6j 9864321 i = 7 j

14 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] 1329648 i = 1j i

15 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

16 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 1329648 8329641 8349621 8649321 8964321 8694321 9864321 9864321

17  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


Download ppt "CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis."

Similar presentations


Ads by Google