180
Advanced Algorithms: Sorting Damian Gordon

Advanced Sorting Algorithms

Embed Size (px)

Citation preview

Page 1: Advanced Sorting Algorithms

Advanced Algorithms: Sorting

Damian Gordon

Page 2: Advanced Sorting Algorithms

Advanced Algorithms

• Sorting Algorithms– Insertion Sort– Shell Sort– Merge Sort – Quick Sort

Page 3: Advanced Sorting Algorithms

We’ll remember …

Page 4: Advanced Sorting Algorithms

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

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

Insertion Sort

Page 7: Advanced Sorting Algorithms

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

Insertion Sort

• Let’s look at an example:

Page 9: Advanced Sorting Algorithms

Insertion Sort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 10: Advanced Sorting Algorithms

Insertion Sort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 11: Advanced Sorting Algorithms

Insertion Sort

23 44 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 12: Advanced Sorting Algorithms

Insertion Sort

23 44 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 13: Advanced Sorting Algorithms

Insertion Sort

23 44 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 14: Advanced Sorting Algorithms

Insertion Sort

23 42 44 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 15: Advanced Sorting Algorithms

Insertion Sort

23 42 44 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 16: Advanced Sorting Algorithms

Insertion Sort

23 42 44 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 17: Advanced Sorting Algorithms

Insertion Sort

23 33 42 44 16 54 34 180 1 2 3 4 5 6 7Age

Page 18: Advanced Sorting Algorithms

Insertion Sort

23 33 42 44 16 54 34 180 1 2 3 4 5 6 7Age

Page 19: Advanced Sorting Algorithms

Insertion Sort

23 33 42 44 16 54 34 180 1 2 3 4 5 6 7Age

Page 20: Advanced Sorting Algorithms

Insertion Sort

16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age

Page 21: Advanced Sorting Algorithms

Insertion Sort

16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age

Page 22: Advanced Sorting Algorithms

Insertion Sort

16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age

Page 23: Advanced Sorting Algorithms

Insertion Sort

16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age

Page 24: Advanced Sorting Algorithms

Insertion Sort

16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age

Page 25: Advanced Sorting Algorithms

Insertion Sort

16 23 33 42 44 54 34 180 1 2 3 4 5 6 7Age

Page 26: Advanced Sorting Algorithms

Insertion Sort

16 23 33 34 42 44 54 180 1 2 3 4 5 6 7Age

Page 27: Advanced Sorting Algorithms

Insertion Sort

16 23 33 34 42 44 54 180 1 2 3 4 5 6 7Age

Page 28: Advanced Sorting Algorithms

Insertion Sort

16 23 33 34 42 44 54 180 1 2 3 4 5 6 7Age

Page 29: Advanced Sorting Algorithms

Insertion Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 30: Advanced Sorting Algorithms

Insertion Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 31: Advanced Sorting Algorithms

Insertion Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 32: Advanced Sorting Algorithms

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

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

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

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

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

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

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

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

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

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

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

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

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

Shell Sort

Page 46: Advanced Sorting Algorithms

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

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

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

Shell Sort

• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].

Page 50: Advanced Sorting Algorithms

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Merge Sort

Page 83: Advanced Sorting Algorithms

Merge Sort

• Merge Sort was developed by John von Neumann in 1945.

Page 84: Advanced Sorting Algorithms

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

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

Merge Sort

• Let’s look at an example:

Page 87: Advanced Sorting Algorithms

Merge Sort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 88: Advanced Sorting Algorithms

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

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

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

Merge Sort

44 23 42 330 1 2 3

16 54 34 184 5 6 7

Page 92: Advanced Sorting Algorithms

Merge Sort

44 23 42 330 1 2 3

16 54 34 184 5 6 7

44 230 1

Page 93: Advanced Sorting Algorithms

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

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

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

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

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

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

Merge Sort

23 440 1

33 422 3

16 544 5

18 346 7

Page 100: Advanced Sorting Algorithms

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

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

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

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

Merge Sort

23 330 1

42 442 3

16 184 5

34 546 7

Page 105: Advanced Sorting Algorithms

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

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

PROGRAM MainMergeSort:

Array = [44,23,42,33,16,54,34,18]; MergeSort(Array); PRINT Array;

END.

Merge Sort

Page 108: Advanced Sorting Algorithms

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

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

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

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

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

Quick Sort

Page 114: Advanced Sorting Algorithms

QuickSort

• Quicksort was developed by Tony Hoare in 1959 and is still a commonly used algorithm for sorting.

Page 115: Advanced Sorting Algorithms

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

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

QuickSort

• Let’s look at an example:

Page 118: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 119: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 120: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 121: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 122: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 123: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 124: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 125: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 126: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 127: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 128: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 129: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 130: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 131: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age

LEFT RIGHT

Page 132: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age

LEFTRIGHT

Page 133: Advanced Sorting Algorithms

QuickSort

44 23 42 33 16 18 34 540 1 2 3 4 5 6 7Age

LEFTRIGHT

Page 134: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 18 44 540 1 2 3 4 5 6 7Age

LEFTRIGHT

Page 135: Advanced Sorting Algorithms

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

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

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

QuickSort

34 23 42 33 16 18 44 540 1 2 3 4 5 6 7Age

THIS IS ALREADYSORTED!!!

Page 139: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age 44 54

6 7

Page 140: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age 44 54

6 7

Page 141: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 142: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 143: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 144: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 145: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 146: Advanced Sorting Algorithms

QuickSort

34 23 42 33 16 180 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 147: Advanced Sorting Algorithms

QuickSort

34 23 18 33 16 420 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 148: Advanced Sorting Algorithms

QuickSort

34 23 18 33 16 420 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 149: Advanced Sorting Algorithms

QuickSort

34 23 18 33 16 420 1 2 3 4 5Age

LEFT RIGHT

44 546 7

Page 150: Advanced Sorting Algorithms

QuickSort

34 23 18 33 16 420 1 2 3 4 5Age

LEFTRIGHT

44 546 7

Page 151: Advanced Sorting Algorithms

QuickSort

34 23 18 33 16 420 1 2 3 4 5Age

LEFTRIGHT

44 546 7

Page 152: Advanced Sorting Algorithms

QuickSort

34 23 18 33 16 420 1 2 3 4 5Age

LEFTRIGHT

44 546 7

Page 153: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age

LEFTRIGHT

44 546 7

Page 154: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 155: Advanced Sorting Algorithms

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

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

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

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 159: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 160: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 161: Advanced Sorting Algorithms

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

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

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

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 165: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 166: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

LEFT RIGHT

Page 167: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

LEFTRIGHT

Page 168: Advanced Sorting Algorithms

QuickSort

16 23 18 33 34 420 1 2 3 4 5Age 44 54

6 7

LEFTRIGHT

Page 169: Advanced Sorting Algorithms

QuickSort

16 18 23 33 34 420 1 2 3 4 5Age 44 54

6 7

LEFTRIGHT

Page 170: Advanced Sorting Algorithms

QuickSort

16 18 23 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 171: Advanced Sorting Algorithms

QuickSort

16 18 23 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 172: Advanced Sorting Algorithms

QuickSort

16 18 23 33 34 420 1 2 3 4 5Age 44 54

6 7

Page 173: Advanced Sorting Algorithms

QuickSort

16 18 23 33 34 420 1 2 3 4 5Age 44 54

6 7

SORTED!

Page 174: Advanced Sorting Algorithms

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

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

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

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

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

Advanced Algorithms

• Sorting Algorithms– Insertion Sort– Shell Sort– Merge Sort – Quick Sort

Page 180: Advanced Sorting Algorithms

etc.