Download presentation
Presentation is loading. Please wait.
1
CS212: Data Structures and Algorithms
Sorting Algorithms
2
Outline Simple Sort Selection Sort Insertion Sort
Exchange (Bubble) Sort
3
Simple Sort – Algorithm
Get a list of unsorted numbers Repeat steps 3 through 6 until the unsorted list is empty Compare the unsorted numbers Select the smallest unsorted number Move this number to the sorted list Store a maximum value in the place of the smallest number Stop 3
4
Simple Sort Unsorted Array Sorted Array 4
5
Simple Sort Unsorted Array Sorted Array 5
6
Simple Sort Unsorted Array Sorted Array 6
7
Simple Sort Unsorted Array Sorted Array 7
8
Simple Sort Unsorted Array Sorted Array 8
9
Simple Sort Unsorted Array Sorted Array 9
10
Simple Sort Unsorted Array Sorted Array 10
11
Simple Sort Unsorted Array Sorted Array 11
12
Selection Sort Find minimum element in the list and also the index of the minimum element. Min_Index = 0 for (I = start; I <= end - 1; I++) if (A[ Min_Index] > A[ I]) Min_Index = I; 12
13
Selection Sort Swap two elements of the list Swap(I, j) {
temp = A[ I ] ; A[ I ] = A[ j ]; A[ j ] = temp; } 13
14
Selection Sort The complete algorithm is: for i ← 1 to n-1 do
min j ← i; min x ← A[i] for j ← i + 1 to n do If A[j] < min x then min j ← j min x ← A[j] A[min j] ← A [i] A[i] ← min x 14
15
Selection Sort Unsorted Array
Swap first smallest i.e. 2 with first array location i.e. 7 15
16
Selection Sort Unsorted Array Swap second smallest i.e. 3 with second
array location i.e. 8 16
17
Selection Sort Unsorted Array
Swap third smallest i.e 4 with third array location i.e. 5 17
18
Selection Sort Unsorted Array Swap fourth smallest i.e. 5 with fourth
array location i.e. 7 18
19
Selection Sort Unsorted Array
Swap fifth smallest i.e. 6 with fifth array location i.e. 7 19
20
Selection Sort Unsorted Array
Swap sixth smallest i.e. 7 with sixth array location i.e. 7 20
21
Selection Sort Sorted Array 21
22
Insertion Sort – Algorithm
Get a list of unsorted numbers Set a marker for the sorted section after the first number in the list Repeat steps 4 through 6 until the unsorted section is empty Select the first unsorted number Swap this number to the left until it arrives at the correct sorted position Advance the marker to the right one position Stop 22
23
Insertion Sort 23
24
Insertion Sort 24
25
Insertion Sort for i ← 2 to n do key ← A[i] j ← i – 1
for i ← 2 to n do key ← A[i] j ← i – 1 while j > 0 and A[j] > key do A[j+1] ← A[j] j ← j – 1 A[j+1] ← key 25
26
Insertion Sort ← j After while loop j = 1 so A[2] ← 108
Start sorting... ← j i → 66 108 56 14 89 12 34 108 1 n key After while loop j = 1 so A[2] ← 108 26
27
Insertion Sort ← j Enter while loop shift up 108 Continue sorting...
66 108 56 14 89 12 34 56 1 n key Enter while loop shift up 108 27
28
Insertion Sort ← j Shifting i → 66 108 56 14 89 12 34 56 1 n key 66
28
29
Insertion Sort ← j Renter while loop shift up 66 Continue i → 66 108
14 89 12 34 56 1 n key Renter while loop shift up 66 29
30
Insertion Sort ← j Shifting i → 66 108 108 14 89 12 34 56 1 n key 66
30
31
Insertion Sort ← j Exit while loop A[1] ← key Continue sorting... i →
66 66 108 14 89 12 34 56 1 n key Exit while loop A[1] ← key 31
32
Insertion Sort ← j Insert Key i → 66 66 108 14 89 12 34 56 1 n key 56
32
33
Insertion Sort ← j Continue sorting... i → 56 66 108 14 89 12 34 14 1
key 33
34
Insertion Sort ← j Continue sorting... i → 14 56 66 108 89 12 34 89 1
key 34
35
Insertion Sort Continue sorting... 14 56 66 89 108 12 34 12 14 34 56
35
36
Exchange (Bubble) Sort
for i ← n to 2 do for j ← 1 to i – 1 do if A[j] > A[j + 1] then A[j] ↔ A[j + 1] 36
37
Exchange (Bubble) Sort
Start sorting... ← i j → 66 108 56 14 89 12 34 1 n 37
38
Exchange (Bubble) Sort
Continue sorting... j → ← i 66 108 56 14 89 12 34 1 n 66 56 108 14 89 12 34 So on .. 38
39
Exchange (Bubble) Sort
Continue sorting... ← i j → 66 56 14 89 12 108 34 1 n 66 56 14 89 12 34 108 39
40
Exchange (Bubble) Sort
Continue sorting... j → ← i 66 56 14 89 12 34 108 1 n 40
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.