Title: Quicksort
1Quicksort
- quick sort orders a list of values by
partitioning the list around one element called a
pivot, then sorting each partition - invented by British computer scientist C.A.R.
Hoare in 1960 - more specifically
- choose one element in the list to be the pivot (
partition element) - organize the elements so that all elements less
than the pivot are to its left and all greater
are to its right - apply the quick sort algorithm (recursively) to
both partitions - Divide and conquer strategy
2Choosing a pivot
- first element
- bad if input is sorted or in reverse sorted order
- bad if input is nearly sorted
- variation particular element (e.g. middle
element) - random element
- median of three elements
- choose the median of the left, right, and center
elements
3Quicksort
4Quicksort
L
R
Mid (LR)/2
5Quicksort
Median of Three if(ArrL gt ArrMid)
Swap(Arr,L,Mid)
L
R
Mid (LR)/2
6Quicksort
Median of Three if(ArrMid gt ArrR)
Swap(Arr,Mid,R)
L
R
Mid (LR)/2
7Quicksort
Median of Three if(ArrMid gt ArrR)
Swap(Arr,Mid,R) // Largest of three in R
L
R
Mid (LR)/2
8Quicksort
Median of Three if(ArrMid gt ArrL)
Swap(Arr,Mid,L)
L
R
Mid (LR)/2
9Quicksort
Median of Three if(ArrMid gt ArrL)
Swap(Arr,Mid,L) // Median of three in L //
Smallest of three in Mid
L
R
Mid (LR)/2
10Quicksort
Partition - Smallest to left of Median, Largest
to right // initialise i L 1 // ignore
median j R 1 // know R is bigger so ignore
i
j
11Quicksort
Partition - Smallest to left of Median, Largest
to right // find item bigger than
median while(Arri lt ArrL) i // find item
bigger than median while(Arrj gt ArrL) j--
i
j
L
12Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
i
j
L
13Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
i
j
L
14Quicksort
Partition - Smallest to left of Median, Largest
to right // find item bigger than
median while(Arri lt ArrL) i // find item
bigger than median while(Arrj gt ArrL) j--
i
j
L
15Quicksort
Partition - Smallest to left of Median, Largest
to right // find item bigger than
median while(Arri lt ArrL) i // find item
bigger than median while(Arrj gt ArrL) j--
i
j
L
16Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
i
j
L
17Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
j
i
L
18Quicksort
Partition - Smallest to left of Median, Largest
to right // Put Median in FINAL
position swap(Arr,L,j)
j
i
L
19Quicksort
Partition - Smallest to left of Median, Largest
to right // Put Median in FINAL
position swap(Arr,L,j)
Right Partition
Left Partition
20Special cases
- What happens when the array contains many
duplicate elements? - What happens when the array is already sorted (or
nearly sorted) to begin with? - Small arrays
- Quicksort is slower than insertion sort when is N
is small (say, N ? 20). - Optimization Make A ? 20 the base case and use
insertion sort algorithm on arrays of that size
or smaller.
21MedianOfThree Algorithm
- public static void MedianOfThree(int List, int
Left, int Right) - int Mid (LeftRight) / 2
- if(ListLeft gt ListMid)
- swap(List, Left, Mid)
-
- if(ListMid gt ListRight)
- swap(List, Mid, Right)
-
- if(ListLeft lt ListMid)
- swap(List, Left, Mid)
-
- swap(List, Left1, Mid)
22Quicksort Algorithm
- private static void swap(int List, int i, int
j) - int temp Listi
- Listi Listj
- Listj temp
23Partition Algorithm
- private static int partition(int List, int
Left, int Right) - MedianOfThree(List, Left, Right)
- int i Left 1
- int j Right 1
- while (i lt j)
- while (Listi lt ListLeft) i //
find big item on way up - while (Listj gt ListLeft) j-- // find
small item on way down if (i lt j)
// If haven't crossed then
swap - swap(List, i, j)
-
-
- swap(List, Left, j) // swap with partition
element - return j
24Quicksort Algorithm
- public static void quicksort(int List, int
Left, int Right) - int ListSize Right - Left 1
- if (ListSize gt QuickSortLowBoundary)
- int p partition(List, Left, Right)
- quicksort(List, Left, p-1)
- quicksort(List, p1, Right)
- else if(ListSize gt 1)
- SelectionSort(List, Left, Right)
-
-