advanced sorting algorithms
TRANSCRIPT
![Page 1: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/1.jpg)
Advanced Algorithms: Sorting
Damian Gordon
![Page 2: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/2.jpg)
Advanced Algorithms
• Sorting Algorithms– Insertion Sort– Shell Sort– Merge Sort – Quick Sort
![Page 3: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/3.jpg)
We’ll remember …
![Page 4: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/4.jpg)
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18};
FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO N-2 DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; ENDIF; ENDFOR;
ENDFOR;END.
Sorting: Bubble Sort
![Page 5: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/5.jpg)
PROGRAM SelectionSort: Integer Age[8] <- {44,23,42,33,16,54,34,18};
FOR Outer-Index IN 0 TO N-1 MinValueLocation <- Outer-Index; FOR Index IN Outer-Index+1 TO N-1 DO IF (Age[Index] < Age[MinValueLocation]) THEN MinValueLocation <- Index; ENDIF; ENDFOR; IF (MinValueLocation != Outer-Index) THEN Swap(Age[Outer-Index], Age[MinValueLocation]); ENDIF;
ENDFOR;END.
Sorting: Selection Sort
![Page 6: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/6.jpg)
Insertion Sort
![Page 7: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/7.jpg)
Insertion Sort
• Insertion Sort works by taking the first two elements of the list, sorting them, then taking the third one, and sorting that into the first two, then taking the fourth element and sorting that into the first three, etc.
![Page 8: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/8.jpg)
Insertion Sort
• Let’s look at an example:
![Page 9: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/9.jpg)
Insertion Sort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 10: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/10.jpg)
Insertion Sort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 11: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/11.jpg)
Insertion Sort
23 44 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 12: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/12.jpg)
Insertion Sort
23 44 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 13: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/13.jpg)
Insertion Sort
23 44 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 14: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/14.jpg)
Insertion Sort
23 42 44 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 15: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/15.jpg)
Insertion Sort
23 42 44 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 16: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/16.jpg)
Insertion Sort
23 42 44 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 17: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/17.jpg)
Insertion Sort
23 33 42 44 16 54 34 180 1 2 3 4 5 6 7Age
![Page 18: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/18.jpg)
Insertion Sort
23 33 42 44 16 54 34 180 1 2 3 4 5 6 7Age
![Page 19: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/19.jpg)
Insertion Sort
23 33 42 44 16 54 34 180 1 2 3 4 5 6 7Age
![Page 20: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/20.jpg)
Insertion Sort
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
![Page 21: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/21.jpg)
Insertion Sort
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
![Page 22: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/22.jpg)
Insertion Sort
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
![Page 23: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/23.jpg)
Insertion Sort
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
![Page 24: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/24.jpg)
Insertion Sort
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
![Page 25: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/25.jpg)
Insertion Sort
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
![Page 26: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/26.jpg)
Insertion Sort
16 23 33 34 42 44 54 180 1 2 3 4 5 6 7Age
![Page 27: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/27.jpg)
Insertion Sort
16 23 33 34 42 44 54 180 1 2 3 4 5 6 7Age
![Page 28: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/28.jpg)
Insertion Sort
16 23 33 34 42 44 54 180 1 2 3 4 5 6 7Age
![Page 29: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/29.jpg)
Insertion Sort
16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age
![Page 30: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/30.jpg)
Insertion Sort
16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age
![Page 31: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/31.jpg)
Insertion Sort
16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age
![Page 32: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/32.jpg)
Insertion Sort
• How do we move the elements into their correct position (we’ll call this the “Insertion Sort move”)?
![Page 33: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/33.jpg)
Insertion Sort
• We store 34 in CURRENT.
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
Current: 34
![Page 34: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/34.jpg)
Insertion Sort
• Next we move what value is in the previous position to 34 into that location.
16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age
Current: 34
![Page 35: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/35.jpg)
Insertion Sort
• Next we move what value is in the previous position to 34 into that location.
16 23 33 42 44 54 54 180 1 2 3 4 5 6 7Age
Current: 34
![Page 36: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/36.jpg)
Insertion Sort
• And we do that again.
16 23 33 42 44 44 54 180 1 2 3 4 5 6 7Age
Current: 34
![Page 37: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/37.jpg)
Insertion Sort
• And again.
16 23 33 42 42 44 54 180 1 2 3 4 5 6 7Age
Current: 34
![Page 38: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/38.jpg)
Insertion Sort
• And then we move CURRENT into the correct position.
16 23 33 42 42 44 54 180 1 2 3 4 5 6 7Age
Current: 34
![Page 39: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/39.jpg)
Insertion Sort
• And then we move CURRENT into the correct position.
16 23 33 42 42 44 54 180 1 2 3 4 5 6 7Age
Current: 34
![Page 40: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/40.jpg)
Insertion Sort
• And then we move CURRENT into the correct position.
16 23 33 34 42 44 54 180 1 2 3 4 5 6 7Age
![Page 41: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/41.jpg)
Insertion Sort
• The element being added in each time is just to the left of the sorted list.
• So, if the next element is called CURRENT, the largest element in the sorted list is CURRENT – 1.
• So we’ll know if the next element is largest if it is bigger than CURRENT – 1.
Sorted sub-list Nextelement
CURRENT
![Page 42: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/42.jpg)
Insertion Sort• Structured English:
FOR each element from the second TO the end of the list DO Remember the current position and value WHILE the previous element is bigger than current DO Move the previous element into current’s position END WHILE We’ve reached the position in the list that current should be Put it inEND FOR
NOTE: The Previous Element is the end ofthe sorted sub-list
![Page 43: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/43.jpg)
PROGRAM InsertionSort: Integer Array[8] <- {44,23,42,33,16,54,34,18};
FOR Index IN 1 TO N DO current = Array[index]; pos = index; WHILE (pos > 0 and Array[pos – 1] > current) DO Array[pos] <- Array[pos - 1]; pos = pos - 1; ENDWHILE; Array[pos] = current;ENDFOR;
END.
Insertion Sort
NOTE: If you havereached the start ofthe list, STOP!
![Page 44: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/44.jpg)
Insertion Sort
• Complexity of Insertion Sort
– Best-case scenario complexity = O(N)– Average complexity = O(N2)– Worst-case scenario complexity = O(N2)
![Page 45: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/45.jpg)
Shell Sort
![Page 46: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/46.jpg)
Shell Sort
• ShellSort is an extension of InsertionSort that was developed by Donald Shell.
• Shell observed that the process of doing the Insertion Sort move, particularly if that have to be moved from one side of the array to other, is computationally expensive.
![Page 47: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/47.jpg)
Shell Sort
• Instead of sorting the whole list, ShellSort first picks ever Nth element, sorts those elements (0, N, 2N, 3N,…), then sorts (1, N+1, 2N+1, 3N+1,…), then (2, N+2, 2N+2, 3N+2,…), and so on.
• Once that’s done, let’s sort every Mth element (where M is N DIV 2), so we’ll sort (0, M, 2M, 3M,…), then sorts (1, M+1, 2M+1, 3M+1,…), then (2, M+2, 2M+2, 3M+2,…), and so on.
• And keep doing this until we get to 1.
![Page 48: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/48.jpg)
Donald L. Shell
• Born: March 1, 1924• Died: November 2, 2015
• Shell, D.L. (1959) "A High-Speed Sorting Procedure“, Communications of the ACM, 2(7), pp. 30–32.
![Page 49: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/49.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
![Page 50: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/50.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
![Page 51: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/51.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
![Page 52: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/52.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18
![Page 53: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/53.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18
![Page 54: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/54.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18– Second group: 23, 54
![Page 55: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/55.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18– Second group: 23, 54
![Page 56: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/56.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18– Second group: 23, 54– Third group: 42, 34
![Page 57: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/57.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18– Second group: 23, 54– Third group: 42, 34
![Page 58: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/58.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18– Second group: 23, 54– Third group: 42, 34– Fourth group: 33, 16
![Page 59: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/59.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18– Second group: 23, 54– Third group: 42, 34– Fourth group: 33, 16
Sort these usingInsertion Sort
![Page 60: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/60.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18 18, 44– Second group: 23, 54 23, 54– Third group: 42, 34 34, 42– Fourth group: 33, 16 16, 33
Sort these usingInsertion Sort
![Page 61: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/61.jpg)
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:– First group: 44, 18 18, 44– Second group: 23, 54 23, 54– Third group: 42, 34 34, 42– Fourth group: 33, 16 16, 33
Sort these usingInsertion Sort
![Page 62: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/62.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Let’s pick every 4th element:– First group: 44, 18 18, 44– Second group: 23, 54 23, 54– Third group: 42, 34 34, 42– Fourth group: 33, 16 16, 33
Sort these usingInsertion Sort
![Page 63: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/63.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Let’s pick every 4th element:– First group: 44, 18 18, 44– Second group: 23, 54 23, 54– Third group: 42, 34 34, 42– Fourth group: 33, 16 16, 33
Sort these usingInsertion Sort
The data is not sorted, but a lot of the big numbers have been moved to the end of the list, and a lot of the smaller numbers have been moved to the start.
![Page 64: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/64.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
![Page 65: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/65.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
![Page 66: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/66.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42
![Page 67: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/67.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42
![Page 68: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/68.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42– Second Group: 23, 16, 54, 33
![Page 69: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/69.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42– Second Group: 23, 16, 54, 33
Sort these usingInsertion Sort
![Page 70: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/70.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42 18, 34, 42, 44– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these usingInsertion Sort
![Page 71: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/71.jpg)
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42 18, 34, 42, 44– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these usingInsertion Sort
![Page 72: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/72.jpg)
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42 18, 34, 42, 44– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these usingInsertion Sort
![Page 73: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/73.jpg)
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Now let’s do every 2nd element:– First Group: 18, 34, 44, 42 18, 34, 42, 44– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these usingInsertion Sort
The data is almost completely sorted now.
![Page 74: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/74.jpg)
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Finally do one more Insertion Sort with all elements
![Page 75: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/75.jpg)
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Finally do one more Insertion Sort with all elements
• This will be very fast since the data is almost sorted
![Page 76: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/76.jpg)
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Finally do one more Insertion Sort with all elements
• This will be very fast since the data is almost sorted
• Age = [16, 18, 23, 33, 34, 42, 44, 54].
![Page 77: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/77.jpg)
Shell Sort
• Let’s look at the PseudoCode in two parts:
– 1) The Insertion Sort code as before, but modified not to sort all of the elements, but rather every Nth element
– 2) The Shell Sort that calls the Insertion Sort code for each Nth elements, and then N/2, and N/4, and so on until 1.
![Page 78: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/78.jpg)
MODULE GapInsertionSort(Array, StartPos, Gap):FOR Index IN StartPos TO N INCREMENT BY Gap DO current = Array[index]; pos = index; WHILE (pos > Gap and Array[pos – Gap] > current) DO Array[pos] <- Array[pos - Gap]; pos = pos - Gap; ENDWHILE; Array[pos] = current;ENDFOR;
END.
Shell Sort
![Page 79: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/79.jpg)
MODULE GapInsertionSort(Array, StartPos, Gap):FOR Index IN StartPos TO N INCREMENT BY Gap DO current = Array[index]; pos = index; WHILE (pos > Gap and Array[pos – Gap] > current) DO Array[pos] <- Array[pos - Gap]; pos = pos - Gap; ENDWHILE; Array[pos] = current;ENDFOR;
END.
Shell Sort
![Page 80: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/80.jpg)
MODULE ShellSort(Array): GapSize <- Length(Array) DIV 2;
WHILE (GapSize > 0) DO FOR StartPos IN 0 TO GapSize DO GapInsertionSort(Array, StartPos, GapSize); ENDFOR; GapSize <- GapSize DIV 2;ENDWHILE;
END.
Shell Sort
We are reducing theGap in half each time
For each of the NthElement, each N+1thElement, N+2th, etc.
The main loop willkeep going until theGap is 1.
![Page 81: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/81.jpg)
Shell Sort
• Complexity of Shell Sort
– Best-case scenario complexity = O(N)– Average complexity = O(N * log2(N))– Worst-case scenario complexity = O(N * log2(N))
![Page 82: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/82.jpg)
Merge Sort
![Page 83: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/83.jpg)
Merge Sort
• Merge Sort was developed by John von Neumann in 1945.
![Page 84: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/84.jpg)
John von Neumann• Born: December 28, 1903• Died: February 8, 1957• Born in Budapest, Austria-Hungary• A mathematician who made major
contributions to set theory, functional analysis, quantum mechanics, ergodic theory, continuous geometry, economics and game theory, computer science, numerical analysis, hydrodynamics and statistics.
![Page 85: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/85.jpg)
Merge Sort
• Merge Sort used a “divide-and-conquer” strategy.
• It’s a two-step process:– 1. Keep splitting the array in half until you end up with sub-arrays of
one item (which are sorted by definition).– 2. Successively merge each sub-array together, and sort with each
merge.
![Page 86: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/86.jpg)
Merge Sort
• Let’s look at an example:
![Page 87: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/87.jpg)
Merge Sort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 88: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/88.jpg)
Merge Sort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7
![Page 89: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/89.jpg)
Merge Sort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7
44 23 42 330 1 2 3
16 54 34 184 5 6 7
![Page 90: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/90.jpg)
Merge Sort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 23 42 330 1 2 3
16 54 34 184 5 6 7
![Page 91: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/91.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
![Page 92: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/92.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 230 1
![Page 93: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/93.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 230 1
23 440 1
![Page 94: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/94.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 230 1
23 440 1
42 332 3
![Page 95: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/95.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 230 1
23 440 1
42 332 3
33 422 3
![Page 96: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/96.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 230 1
23 440 1
42 332 3
33 422 3
16 544 5
16 544 5
![Page 97: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/97.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 230 1
23 440 1
42 332 3
33 422 3
16 544 5
16 544 5
34 186 7
![Page 98: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/98.jpg)
Merge Sort
44 23 42 330 1 2 3
16 54 34 184 5 6 7
44 230 1
23 440 1
42 332 3
33 422 3
16 544 5
16 544 5
34 186 7
18 346 7
![Page 99: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/99.jpg)
Merge Sort
23 440 1
33 422 3
16 544 5
18 346 7
![Page 100: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/100.jpg)
Merge Sort
23 440 1
33 422 3
16 544 5
18 346 7
23 440 1
33 422 3
![Page 101: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/101.jpg)
Merge Sort
23 440 1
33 422 3
16 544 5
18 346 7
23 440 1
33 422 3
23 330 1
42 442 3
![Page 102: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/102.jpg)
Merge Sort
23 440 1
33 422 3
16 544 5
18 346 7
23 440 1
33 422 3
23 330 1
42 442 3
16 544 5
18 346 7
![Page 103: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/103.jpg)
Merge Sort
23 440 1
33 422 3
16 544 5
18 346 7
23 440 1
33 422 3
23 330 1
42 442 3
16 544 5
18 346 7
16 184 5
34 546 7
![Page 104: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/104.jpg)
Merge Sort
23 330 1
42 442 3
16 184 5
34 546 7
![Page 105: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/105.jpg)
Merge Sort
23 330 1
42 442 3
16 184 5
34 546 7
23 330 1
42 442 3
16 184 5
34 546 7
![Page 106: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/106.jpg)
Merge Sort
23 330 1
42 442 3
16 184 5
34 546 7
23 330 1
42 442 3
16 184 5
34 546 7
16 180 1
23 332 3
34 424 5
44 546 7
![Page 107: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/107.jpg)
PROGRAM MainMergeSort:
Array = [44,23,42,33,16,54,34,18]; MergeSort(Array); PRINT Array;
END.
Merge Sort
![Page 108: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/108.jpg)
PROGRAM MergeSort(Array): IF (length(Array) > 1) THEN MidPoint = len(Age)//2 LeftHalf = Age[:MidPoint] RightHalf = Age[MidPoint:]
Merge Sort
Keep recursively splitting the array
until you get down sub-arrays of one
element.
Continued
![Page 109: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/109.jpg)
MergeSort(LeftHalf) MergeSort(RightHalf)
LCounter = 0 RCounter = 0 MainCounter = 0
Merge Sort
Recursively call MergeSort for each half of the array. After the splitting gets down to
one element the recursive calls will pop off the stack to merge
the sub-arrays together.
Continued
Continued
![Page 110: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/110.jpg)
WHILE (LCounter < len(LeftHalf) AND RCounter < len(RightHalf)) DO IF LeftHalf[LCounter] < RightHalf[RCounter] THEN Age[MainCounter] = LeftHalf[LCounter]; LCounter = LCounter + 1; ELSE Age[MainCounter] = RightHalf[RCounter]; RCounter = RCounter + 1; ENDIF; MainCounter = MainCounter + 1; ENDWHILE;
Merge Sort
Continued
Continued Keep comparing each element of the left and right sub-array, writing
the smaller element into the main array
![Page 111: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/111.jpg)
WHILE LCounter < len(LeftHalf) DO Age[MainCounter] = LeftHalf[LCounter]; LCounter = LCounter + 1; MainCounter = MainCounter + 1; ENDWHILE; WHILE Rcounter < len(RightHalf) DO Age[MainCounter] = RightHalf[RCounter] RCounter = RCounter + 1 MainCounter = MainCounter + 1 ENDWHILE;ENDIF;
Merge Sort Continued
After the comparisons are done, write either
the rest of the left array or the right array into the main array that
![Page 112: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/112.jpg)
Merge Sort
• Complexity of Merge Sort
– Best-case scenario complexity = O(N)– Average complexity = O(N * log2(N))– Worst-case scenario complexity = O(N * log2(N))
![Page 113: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/113.jpg)
Quick Sort
![Page 114: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/114.jpg)
QuickSort
• Quicksort was developed by Tony Hoare in 1959 and is still a commonly used algorithm for sorting.
![Page 115: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/115.jpg)
Charles Antony Richard Hoare• Born: January 11, 1934• Hoare's most significant work
includes: his sorting and selection algorithm (Quicksort and Quickselect), Hoare logic, the formal language Communicating Sequential Processes (CSP) used to specify the interactions between concurrent processes, structuring computer operating systems using the monitor concept.
![Page 116: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/116.jpg)
QuickSort
• The key idea behind Quicksort is to pick a random value from the array, and starting from either side of the array, swap elements that are lower than the value in the right of the array with elements of the left of the array that are larger than the value, until we reach the point where the random value should be, then we put the random value in its place.
• We recursively do this process with the sub-arrays on either side of the random value ( called the “pivot”).
![Page 117: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/117.jpg)
QuickSort
• Let’s look at an example:
![Page 118: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/118.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 119: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/119.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
![Page 120: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/120.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 121: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/121.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 122: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/122.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 123: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/123.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 124: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/124.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 125: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/125.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 126: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/126.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 127: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/127.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 128: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/128.jpg)
QuickSort
44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 129: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/129.jpg)
QuickSort
44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 130: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/130.jpg)
QuickSort
44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 131: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/131.jpg)
QuickSort
44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age
LEFT RIGHT
![Page 132: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/132.jpg)
QuickSort
44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age
LEFTRIGHT
![Page 133: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/133.jpg)
QuickSort
44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age
LEFTRIGHT
![Page 134: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/134.jpg)
QuickSort
34 23 42 33 16 18 44 540 1 2 3 4 5 6 7Age
LEFTRIGHT
![Page 135: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/135.jpg)
QuickSort
34 23 42 33 16 18 44 540 1 2 3 4 5 6 7Age
Pivot value is now in its correct
position.
![Page 136: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/136.jpg)
QuickSort
34 23 42 33 16 18 44 540 1 2 3 4 5 6 7Age
Now repeat this process with this
sub-array
![Page 137: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/137.jpg)
QuickSort
34 23 42 33 16 18 44 540 1 2 3 4 5 6 7Age
Now repeat this process with this
sub-array…and this one
![Page 138: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/138.jpg)
QuickSort
34 23 42 33 16 18 44 540 1 2 3 4 5 6 7Age
THIS IS ALREADYSORTED!!!
![Page 139: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/139.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age 44 54
6 7
![Page 140: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/140.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age 44 54
6 7
![Page 141: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/141.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 142: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/142.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 143: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/143.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 144: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/144.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 145: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/145.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 146: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/146.jpg)
QuickSort
34 23 42 33 16 180 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 147: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/147.jpg)
QuickSort
34 23 18 33 16 420 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 148: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/148.jpg)
QuickSort
34 23 18 33 16 420 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 149: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/149.jpg)
QuickSort
34 23 18 33 16 420 1 2 3 4 5Age
LEFT RIGHT
44 546 7
![Page 150: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/150.jpg)
QuickSort
34 23 18 33 16 420 1 2 3 4 5Age
LEFTRIGHT
44 546 7
![Page 151: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/151.jpg)
QuickSort
34 23 18 33 16 420 1 2 3 4 5Age
LEFTRIGHT
44 546 7
![Page 152: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/152.jpg)
QuickSort
34 23 18 33 16 420 1 2 3 4 5Age
LEFTRIGHT
44 546 7
![Page 153: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/153.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age
LEFTRIGHT
44 546 7
![Page 154: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/154.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 155: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/155.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age
Now repeat this process with this
sub-array
44 546 7
![Page 156: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/156.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age
Now repeat this process with this
sub-array…and this one
44 546 7
![Page 157: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/157.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age
Now repeat this process with this
sub-array THIS IS ALREADY SORTED
44 546 7
![Page 158: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/158.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 159: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/159.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 160: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/160.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 161: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/161.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
No swaps will occur on this pass, as 16 is in the right
place
![Page 162: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/162.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
…but as 16 is swapped into itself, the rest of the sub-
array will be sorted.
![Page 163: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/163.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
…but as 16 is swapped into itself, the rest of the sub-
array will be sorted.
![Page 164: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/164.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 165: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/165.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 166: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/166.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
LEFT RIGHT
![Page 167: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/167.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
LEFTRIGHT
![Page 168: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/168.jpg)
QuickSort
16 23 18 33 34 420 1 2 3 4 5Age 44 54
6 7
LEFTRIGHT
![Page 169: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/169.jpg)
QuickSort
16 18 23 33 34 420 1 2 3 4 5Age 44 54
6 7
LEFTRIGHT
![Page 170: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/170.jpg)
QuickSort
16 18 23 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 171: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/171.jpg)
QuickSort
16 18 23 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 172: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/172.jpg)
QuickSort
16 18 23 33 34 420 1 2 3 4 5Age 44 54
6 7
![Page 173: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/173.jpg)
QuickSort
16 18 23 33 34 420 1 2 3 4 5Age 44 54
6 7
SORTED!
![Page 174: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/174.jpg)
PROGRAM QuickSort(Array, First, Last):
IF (First < Last) THEN Pivot = Partition(Array, First, Last);
QuickSort(Array, First, Pivot - 1): QuickSort(Array, Pivot + 1, Last): ENDIF;
END.
QuickSort
![Page 175: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/175.jpg)
PROGRAM Partition(Array, First, Last): PivotVal = Array[First]; Finished = False; LeftPointer = First + 1; RightPointer = Last;
QuickSort
We randomly selectthe pivot, in this casewe select the first element. Since the array isn’t sorted yet, the value of the firstelement could haveany value
Continued
![Page 176: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/176.jpg)
WHILE NOT(Finished) DO WHILE (LeftPointer <= RightPointer) AND (Age[LeftPointer] <= PivotVal) DO LeftPointer = LeftPointer + 1 ENDWHILE; WHILE (Age[RightPointer] >= PivotVal) AND (RightPointer >= LeftPointer) DO RightPointer = RightPointer - 1 ENDWHILE;
QuickSort
Continued
Continued
Keep moving left until we find a
value that is less than the pivot, or
we reach the Right Pointer.
Keep moving right until we find a value that is
greater than the pivot, or we reach
the left Pointer.
![Page 177: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/177.jpg)
IF (LeftPointer < RightPointer) THEN Finished = False; ELSE SWAP(Age[LeftPointer], Age[RightPointer]); ENDIF; SWAP(Age[First], Age[RightPointer]);
RETURN RightPointer;
END Partition.
QuickSort Continued
We’ve a value greater than the pivot to the left,
and one less to the right, swap them
Put the pivot in its correct position
![Page 178: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/178.jpg)
QuickSort
• Complexity of Quick Sort
– Best-case scenario complexity = O(N * log2(N))– Average complexity = O(N * log2(N))– Worst-case scenario complexity = O(N2)
![Page 179: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/179.jpg)
Advanced Algorithms
• Sorting Algorithms– Insertion Sort– Shell Sort– Merge Sort – Quick Sort
![Page 180: Advanced Sorting Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021420/58f306431a28ab2e2d8b4649/html5/thumbnails/180.jpg)
etc.