Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The...

24
Quick Sort

Transcript of Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The...

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

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.

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

Quick Sort – Visualization

85 24 63 45 17 9631 50

Quick Sort – Visualization

85 24 63 45 17 9631 50

Quick Sort – Visualization

85 24 63 45 17 9631 50

24 45 17 31 85 966350

Quick Sort – Visualization

85 24 63 45 17 9631 50

24 45 17 31 85 966350

Quick Sort – Visualization

85 24 63 45 17 9631 50

24 45 17 31 85 9663

24 17 45 85 63

50

31 96

Quick Sort – Visualization

85 24 63 45 17 9631 50

24 45 17 31 85 9663

24 17 45 85 63

50

31 96

Quick Sort – Visualization

85 24 63 45 17 9631 50

24 45 17 31 85 9663

24 17 45 85 63

24

50

31 96

17 8563

Quick Sort – Visualization

85 24 63 45 17 9631 50

24 45 17 31 85 9663

17 24 45 63 85

50

31 96

Quick Sort – Visualization

85 24 63 45 17 9631 50

17 24 31 45 63 968550

Quick Sort – Visualization

17 24 31 45 50 8563 96

Efficiency

85 24 63 45 17 9631 50

24 45 17 31 85 9663

24 17 45 85 63

24

50

31 96

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

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.

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.

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.

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

In-Place Quick Sort Visualization

85 24 63 45 17 9631 50

In-Place Quick Sort Visualization

85 24 63 45 17 9631 50rl p

Pivot p = element at the right boundindex l = leftBoundindex r = rightBound – 1while l <= r

31 24 63 45 17 9685 50rl

swap l and r (as long as l < r)

p

85 24 63 45 17 9631 50rl p

l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l

In-Place Quick Sort Visualization

85 24 63 45 17 9631 50rl p

Pivot p = element at the right boundindex l = leftBoundindex r = rightBound – 1while l <= r

31 24 63 45 17 9685 50rl

swap l and r (as long as l < r)

p

31 24 63 45 17 9685 50rl p

31 24 17 45 63 9685 50rl p

85 24 63 45 17 9631 50rl p

l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l

In-Place Quick Sort Visualization

85 24 63 45 17 9631 50rl p

Pivot p = element at the right boundindex l = leftBoundindex r = rightBound – 1while l <= r

31 24 63 45 17 9685 50rl

swap l and r (as long as l < r)

p

31 24 63 45 17 9685 50rl p

31 24 17 45 63 9685 50rl p

31 24 17 45 63 9685 50r l p

l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l

85 24 63 45 17 9631 50rl p

l++ while its value <= p AND l <= r r-- while its value >= p AND r >= l

31 24 17 45 50 9685 63r l

when l > r swap l and p

p

sort(left = leftBound, right = l – 1)sort(left = l + 1, right = rightBound)

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)