Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quick Sort.

Similar presentations


Presentation on theme: "Quick Sort."— Presentation transcript:

1 Quick Sort

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

3 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.

4 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”).

5 QuickSort Let’s look at an example:

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

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

8 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

9 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

10 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

11 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

12 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

13 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

14 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

15 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

16 QuickSort 44 23 1 42 2 33 3 4 16 5 54 6 34 18 7 Age LEFT RIGHT

17 QuickSort 44 23 1 42 2 33 3 4 16 5 18 6 34 54 7 Age LEFT RIGHT

18 QuickSort 44 23 1 42 2 33 3 4 16 5 18 6 34 54 7 Age LEFT RIGHT

19 QuickSort 44 23 1 42 2 33 3 4 16 5 18 6 34 54 7 Age LEFT RIGHT

20 QuickSort 44 23 1 42 2 33 3 4 16 5 18 6 34 54 7 Age LEFT RIGHT

21 QuickSort 44 23 1 42 2 33 3 4 16 5 18 6 34 54 7 Age LEFT RIGHT

22 QuickSort 34 23 1 42 2 33 3 4 16 5 18 6 44 54 7 Age LEFT RIGHT

23 Pivot value is now in its correct position.
QuickSort 34 23 1 42 2 33 3 16 4 18 5 44 6 54 7 Age Pivot value is now in its correct position.

24 Now repeat this process with this sub-array
QuickSort 34 23 1 42 2 33 3 16 4 18 5 44 6 54 7 Age Now repeat this process with this sub-array

25 Now repeat this process with this sub-array
QuickSort 34 23 1 42 2 33 3 4 16 5 18 6 44 54 7 Age Now repeat this process with this sub-array …and this one

26 QuickSort Age 34 23 1 42 2 33 3 16 4 18 5 44 6 54 7 THIS IS ALREADY
34 23 1 42 2 33 3 16 4 18 5 44 6 54 7 Age THIS IS ALREADY SORTED!!!

27 QuickSort 34 1 23 2 42 33 3 4 16 18 5 6 44 7 54 Age

28 QuickSort 34 1 23 2 42 33 3 4 16 18 5 6 44 7 54 Age

29 QuickSort 34 1 23 2 42 3 33 4 16 18 5 6 44 7 54 Age LEFT RIGHT

30 QuickSort 34 1 23 2 42 3 33 4 16 18 5 6 44 7 54 Age LEFT RIGHT

31 QuickSort 34 1 23 2 42 3 33 4 16 18 5 6 44 7 54 Age LEFT RIGHT

32 QuickSort 34 1 23 2 42 3 33 4 16 18 5 6 44 7 54 Age LEFT RIGHT

33 QuickSort 34 1 23 2 42 3 33 4 16 18 5 6 44 7 54 Age LEFT RIGHT

34 QuickSort 34 1 23 2 42 3 33 4 16 18 5 6 44 7 54 Age LEFT RIGHT

35 QuickSort 34 1 23 2 18 3 33 4 16 42 5 6 44 7 54 Age LEFT RIGHT

36 QuickSort 34 1 23 2 18 3 33 4 16 42 5 6 44 7 54 Age LEFT RIGHT

37 QuickSort 34 1 23 2 18 3 33 4 16 42 5 6 44 7 54 Age LEFT RIGHT

38 QuickSort 34 1 23 2 18 3 33 4 16 42 5 6 44 7 54 Age LEFT RIGHT

39 QuickSort 34 1 23 2 18 3 33 4 16 42 5 6 44 7 54 Age LEFT RIGHT

40 QuickSort 34 1 23 2 18 3 33 4 16 42 5 6 44 7 54 Age LEFT RIGHT

41 QuickSort 16 1 23 2 18 3 33 4 34 42 5 6 44 7 54 Age LEFT RIGHT

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

43 Now repeat this process with this sub-array
QuickSort 16 1 23 2 18 3 33 4 34 42 5 6 44 7 54 Age Now repeat this process with this sub-array

44 Now repeat this process with this sub-array
QuickSort 16 1 23 2 18 3 33 4 34 42 5 6 44 7 54 Age Now repeat this process with this sub-array …and this one

45 Now repeat this process with this sub-array
QuickSort 16 1 23 2 18 3 33 4 34 42 5 6 44 7 54 Age Now repeat this process with this sub-array THIS IS ALREADY SORTED

46 QuickSort 16 1 23 2 18 33 3 4 34 42 5 6 44 7 54 Age

47 QuickSort 16 1 23 2 18 33 3 4 34 42 5 6 44 7 54 Age

48 QuickSort 16 1 23 2 18 33 3 4 34 42 5 6 44 7 54 Age

49 No swaps will occur on this pass, as 16 is in the right place
QuickSort 16 1 23 2 18 3 33 4 34 42 5 6 44 7 54 Age No swaps will occur on this pass, as 16 is in the right place

50 QuickSort 16 1 23 2 18 3 33 4 34 42 5 6 44 7 54 Age …but as 16 is swapped into itself, the rest of the sub-array will be sorted.

51 QuickSort 16 1 23 2 18 3 33 4 34 42 5 6 44 7 54 Age …but as 16 is swapped into itself, the rest of the sub-array will be sorted.

52 QuickSort 16 1 23 2 18 33 3 4 34 42 5 6 44 7 54 Age

53 QuickSort 16 1 23 2 18 33 3 4 34 42 5 6 44 7 54 Age

54 QuickSort 16 1 23 2 18 3 33 4 34 42 5 44 6 54 7 Age LEFT RIGHT

55 QuickSort 16 1 23 2 18 3 33 4 34 42 5 44 6 54 7 Age LEFT RIGHT

56 QuickSort 16 1 23 2 18 3 33 4 34 42 5 44 6 54 7 Age LEFT RIGHT

57 QuickSort 16 1 18 2 23 3 33 4 34 42 5 44 6 54 7 Age LEFT RIGHT

58 QuickSort 16 1 18 2 23 33 3 4 34 42 5 6 44 7 54 Age

59 QuickSort 16 1 18 2 23 33 3 4 34 42 5 6 44 7 54 Age

60 QuickSort 16 1 18 2 23 33 3 4 34 42 5 6 44 7 54 Age

61 QuickSort 16 1 18 2 23 3 33 4 34 42 5 6 44 7 54 Age SORTED!

62 QuickSort 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.

63 QuickSort PROGRAM Partition(Array, First, Last): PivotVal = Array[First]; Finished = False; LeftPointer = First + 1; RightPointer = Last; We randomly select the pivot, in this case we select the first element. Since the array isn’t sorted yet, the value of the first element could have any value Continued 

64  Continued QuickSort Keep moving left until we find a value that is less than the pivot, or we reach the Right Pointer. 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 Keep moving right until we find a value that is greater than the pivot, or we reach the left Pointer. Continued 

65 Put the pivot in its correct position
 Continued QuickSort IF (LeftPointer < RightPointer) THEN Finished = False; ELSE SWAP(Age[LeftPointer], Age[RightPointer]); ENDIF; SWAP(Age[First], Age[RightPointer]); RETURN RightPointer; END Partition. 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

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

67 Advanced Algorithms Sorting Algorithms Insertion Sort Shell Sort
Merge Sort Quick Sort

68 etc.


Download ppt "Quick Sort."

Similar presentations


Ads by Google