10. quick sort
TRANSCRIPT
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 1/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 11
DATASTRUCTURE
S
MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 2/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 22
QUICK SORT
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 4/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 44
Quick Sort
x < p p p ≤ x
PartitionFirstPart SecondPart
p
pivot
A:
Recursive call
x < p p p ≤ x
Sorted
FirstPart
Sorted
SecondPart
Sorted Sorted
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 5/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 55
Quick SortQuick Sort
8814
982562
52
79
3023
31
Divide and Conquer
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 6/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 66
Quick SortQuick Sort
8814
982562
52
79
302
3
31
Partition set into two using
randomly chosen pivot
14
2530
2
3
31
8898
6279
≤ 52 ≤
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 7/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 77
Quick SortQuick Sort
14
2530
23
31
88 9862
79≤ 52 ≤
14,23,25,30,31
sort the first half.
62,79,98,88
sort the second half.
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 8/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 88
Quick SortQuick Sort
14,23,25,30,31
62,79,88,98
52
Glue pieces together.
(No real work)
14,23,25,30,31,52,62,79,88,98
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 9/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 99
Quick-SortQuick-Sort
Quick-sortQuick-sort is ais a
randomized sortingrandomized sorting
algorithm based on thealgorithm based on the
divide-and-conquerdivide-and-conquer
paradigm:paradigm:– DivideDivide: pick a random: pick a random
elementelement x x (called(called pivotpivot))
and partitionand partition S S intointo
L L elements less thanelements less than x x
E E elements equalelements equal x x G G elements greater thanelements greater than x x
– RecurRecur: sort: sort L L andand G G
– ConquerConquer: join: join L L,, E E andand G G
x
x
L G E
x
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 10/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1010
Quick SortQuick Sort
8814
982562
52
79
3023
31
Let pivot be the first
element in the list?
14
2530
23
8898
6279
≤ 31 ≤52
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 11/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1111
Quick SortQuick Sort
≤ 14 ≤
14,23,25,30,31,52,62,79,88,98
23,25,30,31,52,62,79,88,98
If the list is already sorted,
then the slit is worst case unbalanced.
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 12/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1212
Partition ExamplePartition Example
A: A: 4 8 6 3 5 1 7 2
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 13/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1313
Partition ExamplePartition Example
A: A: 4 8 6 3 5 1 7 2
i=0i=0
j=1j=1
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 14/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1414
Partition ExamplePartition Example
A: A:
j=1j=1
4 8 6 3 5 1 7 2
i=0i=0
8
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 15/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1515
Partition ExamplePartition Example
A: A: 4 8 6 3 5 1 7 26
i=0i=0
j=2j=2
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 16/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1616
Partition ExamplePartition Example
A: A: 4 8 6 3 5 1 7 2
i=0i=0
383
j=3j=3
i=1i=1
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 17/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1717
Partition ExamplePartition Example
A: A: 4 3 6 8 5 1 7 2
i=1i=1
5
j=4j=4
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 18/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1818
Partition ExamplePartition Example
A: A: 4 3 6 8 5 1 7 2
i=1i=1
1
j=5j=5
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 19/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 1919
Partition ExamplePartition Example
A: A: 4 3 6 8 5 1 7 2
i=2i=2
1 6
j=5j=5
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 20/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2020
Partition ExamplePartition Example
A: A: 4 3 8 5 7 2
i=2i=2
1 6 7
j=6j=6
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 21/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2121
Partition ExamplePartition Example
A: A: 4 3 8 5 7 2
i=2i=2
1 6 22 8
i=3i=3
j=7j=7
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 22/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2222
Partition ExamplePartition Example
A: A: 4 3 2 6 7 8
i=3i=3
1 5
j=8j=8
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 23/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2323
Partition ExamplePartition Example
A: A: 4 1 6 7 8
i=3i=3
2 542 3
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 24/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2424
A: A: 3 6 7 81 542
x < 4x < 4 44 ≤≤ xx
pivot incorrect position
Partition ExamplePartition Example
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 25/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2525
Quick SortQuick SortPicking the pivot:
Median of three
Find the first, middle and last element in the array. Usethe median of the three as the pivot.
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 26/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2626
Partitioning the list:
1. Swap the pivot element with the last element.
8 1 4 9 6 3 5 2 7 0
8 1 4 9 0 3 5 2 7 6
2. Set i to the first element and j to the next to last element.
8 1 4 9 0 3 5 2 7 6i j
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 27/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2727
Partitioning the list:
3. Move i to the right until it refers to an element that islarger than the value at the pivot.
8 1 4 9 0 3 5 2 7 6i j
4. Move j to the left until it refers to an element that issmaller than the value at the pivot.
8 1 4 9 0 3 5 2 7 6i j
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 28/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2828
Partitioning the list:
5. If i is to the left of j, swap the elements at positions i and j.
8 1 4 9 0 3 5 2 7 6i j
2 1 4 9 0 3 5 8 7 6i j
8/8/2019 10. Quick Sort
http://slidepdf.com/reader/full/10-quick-sort 29/30
(C) GOYANI MAHESH(C) GOYANI MAHESH 2929
Partitioning the list:
6. Continue the process until i and j meet or i surpasses j.
2 1 4 9 0 3 5 8 7 6i j
2 1 4 5 0 3 9 8 7 6j i
7. Swap the pivot element and the element at position i.
2 1 4 5 0 3 6 8 7 9
Complexity