Title: Quick Sort
1Quick Sort
2Quicksort
- 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).
3Quick 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.
4Quick 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 lt 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))
5Quick Sort Visualization
6Quick Sort Visualization
7Quick Sort Visualization
85
96
63
8Quick Sort Visualization
85
96
63
9Quick Sort Visualization
85
96
63
45
31
96
10Quick Sort Visualization
85
96
63
45
31
96
11Quick Sort Visualization
85
96
63
45
31
96
24
17
85
63
12Quick Sort Visualization
85
96
63
45
31
96
13Quick Sort Visualization
63
96
85
14Quick Sort Visualization
15Efficiency
n 8
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).
16Efficiency
- 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(Nlog(N)) for
Quicksort.
17Efficiency
- 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(Nlog(N)) comparisons.
18Efficiency
- 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.
19In-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
20In-Place Quick Sort Visualization
21In-Place Quick Sort Visualization
Pivot p element at the right bound index l
leftBound index r rightBound 1
r
l
p
while l lt r
22In-Place Quick Sort Visualization
Pivot p element at the right bound index l
leftBound index r rightBound 1
r
l
p
while l lt r
r
l
p
r
l
p
23In-Place Quick Sort Visualization
Pivot p element at the right bound index l
leftBound index r rightBound 1
r
l
p
while l lt r
l while its value lt p AND l lt r r-- while
its value gt p AND r gt l
r
l
p
swap l and r (as long as l lt r)
r
l
p
l while its value lt p AND l lt r r-- while
its value gt p AND r gt l
r
l
p
when l gt r swap l and p
r
l
p
sort(left leftBound, right l 1) sort(left
l 1, right rightBound)
r
l
p
r
l
p
24Randomized 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)