CS 212 Data Structures and Algorithms Sorting Algorithms
- Slides: 40
CS 212: Data Structures and Algorithms Sorting Algorithms
Outline Simple Sort Selection Sort Insertion Sort Exchange (Bubble) Sort 2
Simple Sort – Algorithm 1. 2. 3. 4. 5. 6. 7. 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
Simple Sort Unsorted Array Sorted Array 4
Simple Sort Unsorted Array Sorted Array 5
Simple Sort Unsorted Array Sorted Array 6
Simple Sort Unsorted Array Sorted Array 7
Simple Sort Unsorted Array Sorted Array 8
Simple Sort Unsorted Array Sorted Array 9
Simple Sort Unsorted Array Sorted Array 10
Simple Sort Unsorted Array Sorted Array 11
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
Selection Sort Swap two elements of the list Swap(I, j) { temp = A[ I ] ; A[ I ] = A[ j ]; A[ j ] = temp; } 13
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
Selection Sort Unsorted Array Swap first smallest i. e. 2 with first array location i. e. 7 15
Selection Sort Unsorted Array Swap second smallest i. e. 3 with second array location i. e. 8 16
Selection Sort Unsorted Array Swap third smallest i. e 4 with third array location i. e. 5 17
Selection Sort Unsorted Array Swap fourth smallest i. e. 5 with fourth array location i. e. 7 18
Selection Sort Unsorted Array Swap fifth smallest i. e. 6 with fifth array location i. e. 7 19
Selection Sort Unsorted Array Swap sixth smallest i. e. 7 with sixth array location i. e. 7 20
Selection Sorted Array 21
Insertion Sort – Algorithm 1. 2. 3. 4. 5. 6. 7. 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
Insertion Sort 23
Insertion Sort 24
Insertion Sort 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
Insertion Sort Start sorting. . . ←j 66 i→ 108 56 14 89 12 1 34 108 n key After while loop j = 1 so A[2] ← 108 26
Insertion Sort Continue sorting. . . ←j 66 108 i→ 56 14 1 89 12 34 56 n key Enter while loop shift up 108 27
Insertion Sort Shifting ←j 66 108 i→ 56 14 89 12 1 66 108 14 89 12 34 56 n key 34 28
Insertion Sort Continue ←j 66 i→ 108 14 89 1 12 34 56 n key Renter while loop shift up 66 29
Insertion Sort Shifting ←j 66 i→ 108 14 89 12 1 66 66 108 14 89 12 34 56 n key 34 30
Insertion Sort Continue sorting. . . ←j i→ 66 66 108 14 1 89 12 34 56 n key Exit while loop A[1] ← key 31
Insertion Sort Insert Key ←j i→ 66 66 108 14 89 12 1 56 66 108 14 89 12 34 56 n key 34 32
Insertion Sort Continue sorting. . . ←j 56 1 66 108 i→ 14 89 12 34 14 n key 33
Insertion Sort Continue sorting. . . ←j 14 1 56 66 108 i→ 89 12 34 89 n key 34
Insertion Sort Continue sorting. . . 14 56 66 89 108 12 34 12 14 34 56 66 89 108 1 n 35
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
Exchange (Bubble) Sort Start sorting. . . ←i j→ 66 1 108 56 14 89 12 34 n 37
Exchange (Bubble) Sort Continue sorting. . . ←i j→ 66 108 56 14 89 12 1 66 So on. . 34 n 56 108 14 89 12 34 38
Exchange (Bubble) Sort Continue sorting. . . ←i j→ 66 56 14 89 12 108 1 66 34 n 56 14 89 12 34 108 39
Exchange (Bubble) Sort Continue sorting. . . ←i j→ 66 1 56 14 89 12 34 108 n 40
- Difference between external and internal sorting
- Professor ajit diwan
- Kevin wayne princeton
- Data structures and algorithms tutorial
- Information retrieval data structures and algorithms
- Data structures and algorithms bits pilani
- Ajit diwan
- Data structures and algorithms
- Data structures and algorithms
- Waterloo data structures and algorithms
- Information retrieval data structures and algorithms
- Data structures and algorithms
- Recursive sorting algorithms
- Quadratic sorting algorithms
- Efficiency of sorting algorithms
- C sorting algorithms
- Complexity of algorithm
- 10 sorting algorithms
- Sorting algorithms with examples
- N
- Most common sorting algorithms
- Introduction to sorting algorithms
- Insertion sort decision tree 4 elements
- Algorithms + data structures = programs
- Biology homology
- Orale charakterstruktur
- Tujuan utama dari proses pengurutan (sorting) data adalah:
- Tujuan melakukan sorting data di dokumen adalah
- Sorting dan searching
- Metode sorting berdasarkan perbandingan adalah
- Physics 212 gradebook
- Physics 212 gradebook
- Pese 212
- Sungguh inginkah engkau lakukan
- Ienf-212
- Et 212
- Et 212
- Arm reach 212 cm on tiptoes
- Archimedes ( arşimet) (mö 287–212 )
- 287-212
- Phys 212 equation sheet