Chapter9 Part3
Chapter9 Part3
Sorting:
Quicksort and Mergesort
Properties of a Sorted Array
What properties does a sorted array have?
1 2 3 4 5 6 7 8
≤n n ≥n
For any value n
All elements to the left of n are less than or equal to n
All elements to the right of n are greater than or equal to n
Pivot 6 7 2 5 1 8 3 4
2 5 1 3 4 6 7 8
Quicksort
Now, apply the same process to the left and right sides of 6
2 5 1 3 4 6 7 8
1 2 5 3 4 6 7 8
Now 2 and 7 are both in their sorted positions
There is no need to move either 2 or 7 again
We repeat this process until every sub-array is of size 1
When that point is reached, the array will be sorted
This recursive sorting algorithm is known as quicksort
Quicksort
quicksort(data[])
if data.length > 1
choose pivot;
while there is an element el left in data
if (el ≤ pivot) add el to data1[];
else if (el ≥ pivot) add el to data2[];
quicksort(data1[]); // recursive call
quicksort(data2[]); // recursive call
6 7 2 5 1 8 3 4
8 7 2 6 1 5 3 4
Null pointer
exception
while (pivot > data[lower]) lower++;
Simple trick to prevent null pointers
Pre-processing step before the first call to the recursive function
Find the largest element and swap with the last element
6 7 2 4 1 5 3 8
Ensures there’s at least one element to stop the while loop
How does this impact efficiency?
while (pivot > data[lower]) lower++;
Quicksort: Example while (pivot < data[upper]) upper--;
2 4 9 5 5 6 3 7 8 2 Pivot
Largest
to to
thethe
first
lastcell
cell
2 3 2 Partitioning
Pivot to upper’s cell 7 6 8
2 3 6 8
2 2 3 4 5 5 6 7 8 9
Quicksort: Efficiency
Best case: Selected pivots create Worst case: Selected pivots create
two even-size arrays at every one empty array at every
partitioning partitioning
n n
n/2 n/2 n–1
n/4 n/4 n/4 n/4 n–2
? ?
……………………………….. …………………………..
1 1 ………………. 1 1 1
(n–1) n n2 – n
n + n + … + n = n lg n (n–1) + (n–2) + … + 1 = =
2 2
How many times can we Quicksort
divide n by 2?
Best Average Worst
lg n times
Therefore (lg n) + 1 total subsets O(n lg n) O(n lg n) O(n2)
Mergesort
Quicksort is efficient, but rarely performs optimally
This is because perfect pivots are hard to find
Quicksort partitions the array, then sorts each partition
Can we partition perfectly, then merge partitions while sorting?
n=8 If we always
halve the
4 4 arrays, we
2 2 2 2 ensure
logarithmic
Merge! 1 1 1 1 1 1 1 1 performance
sorted 2 2 2 2
How are we
going to merge
sorted 4 4 the sub-arrays
so that they’re
sorted?
sorted 8
Mergesort Can we do this
without the temp
array?
array
2 5 6 8 1 5 7 9 Look at 1, which is
copied to the 1st array
position
Mergesort 2 4 9 5 5 7 3 6 2
Best O(n lg n)
Average O(n lg n)
2 4 9 5 5 7 3 6 2
Worst O(n lg n) 2 4 9 5 5 7 3 6 2
2 4
Unfortunately,
mergesort is 2 4
memory-wasteful
2 4 9 5 5 3 7 2 6
2 4 5 5 9 2 3 6 7
mergeSort(array[], first, last)
if first < last
mid = (first + last) / 2; 2 2 3 4 5 5 6 7 9
mergeSort(array, first, mid);
mergeSort(array, mid + 1, last);
merge(data, first, last);