Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively.

Similar presentations


Presentation on theme: "Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively."— Presentation transcript:

1 Quick Sort

2 Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively recursive sort. It's essentially a faster in-place version of the merge sort. The quick sort algorithm is simple in theory, but very difficult to put into code (computer scientists tied themselves into knots for years trying to write a practical implementation of the algorithm, and it still has that effect on university students).

3 Quick Sort The recursive algorithm consists of four steps (which closely resemble the merge sort): – If there are one or less elements in the array to be sorted, return immediately. – Pick an element in the array to serve as a "pivot" point. (Usually the right-most element in the array is used.) – Split the array into two parts - one with elements larger than the pivot and the other with elements smaller than the pivot. – Recursively repeat the algorithm for both halves of the original array.

4 Quick Sort function quicksort(q) var list less, pivotList, greater if length(q) ≤ 1return q select a pivot value pivot from q for each x in q except the pivot element if x < pivot then add x to less if x ≥ pivot then add x to greater add pivot to pivotList return concatenate(quicksort(less), pivotList, quicksort(greater))

5 Quick Sort – Visualization 8524634517963150

6 Quick Sort – Visualization 8524634517963150

7 Quick Sort – Visualization 852463451796315024451731 859663 50

8 Quick Sort – Visualization 852463451796315024451731 859663 50

9 Quick Sort – Visualization 852463451796315024451731 859663 2417 45 8563 50 3196

10 Quick Sort – Visualization 852463451796315024451731 859663 2417 45 8563 50 3196

11 Quick Sort – Visualization 852463451796315024451731 859663 2417 45 8563 24 50 3196 17 8563

12 Quick Sort – Visualization 852463451796315024451731 859663 1724 45 6385 50 3196

13 Quick Sort – Visualization 852463451796315017243145 639685 50

14 Quick Sort – Visualization 1724314550856396

15 Efficiency 8524634517963150 24451731859663 2417458563 24 50 3196 17 8563 Assuming the divide steps and the conquer steps take time proportional to n, then the runtime of each level is proportional to n. With logn + 1 levels, the runtime is O(nlogn). n = 8

16 Efficiency Best Case Situation Assuming that the list breaks into two equal halves, we have two lists of size N/2 to sort. In order for each half to be partitioned, (N/2)+(N/2) = N comparisons are made. Also assuming that each of these list breaks into two equal sized sublists, we can assume that there will be at the most log(N) splits. This will result in a best time estimate of O(N*log(N)) for Quicksort.

17 Efficiency Worst Case Situation In the worst case the list does not divide equally and is larger on one side than the other. In this case the splitting may go on N-1 times. This gives a worst-case time estimate of O(N²). Average Time The average time for Quicksort is estimated to be O(N*log(N)) comparisons.

18 Efficiency The disadvantage of the simple version previously stated is that it requires extra storage space. The additional memory allocations required can also drastically impact speed and cache performance in practical implementations. There is a more complicated version which uses an in-place partition algorithm.

19 In-Place Quick Sort Sorting in place – “Instead of transferring elements out of a sequence and then back in, we just re-arrange them.” – Uses a constant amount of memory – Efficient space usage Algorithm inPlaceQuickSort – Runs efficiently when the sequence is implemented with an array

20 In-Place Quick Sort Visualization 8524634517963150

21 In-Place Quick Sort Visualization 8524634517963150 rlp Pivot p = element at the right bound index l = leftBound index r = rightBound – 1 while l <= r 3124634517968550 rl swap l and r (as long as l < r) p 8524634517963150 rlp l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l

22 In-Place Quick Sort Visualization 8524634517963150 rlp Pivot p = element at the right bound index l = leftBound index r = rightBound – 1 while l <= r 3124634517968550 rl swap l and r (as long as l < r) p 3124634517968550 rlp 3124174563968550 rl p 8524634517963150 rlp l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l

23 In-Place Quick Sort Visualization 8524634517963150 rlp Pivot p = element at the right bound index l = leftBound index r = rightBound – 1 while l <= r 3124634517968550 rl swap l and r (as long as l < r) p 3124634517968550 rlp 3124174563968550 rl p 3124174563968550 rl p l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l 8524634517963150 rlp l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l 3124174550968563 rl when l > r swap l and p p sort(left = leftBound, right = l – 1) sort(left = l + 1, right = rightBound)

24 Randomized Quick Sort Variation of the quick sort algorithm Instead of selecting the last element as the pivot, select an element at random Expected runtime efficiency will always be O(nlogn)


Download ppt "Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively."

Similar presentations


Ads by Google