quicksort quicksort 7 4 9 6 2 2 4 6 7 9 4 2 2 47 9 7 9 2 29 9
TRANSCRIPT
QuicksortQuicksort7 4 9 6 2 2 4 6 7 9
4 2 2 4 7 9 7 9
2 2 9 9
Brute Force Brute Force – – Algorithm Design Algorithm Design
StrategyStrategy• Brute force - A straightforward approach to solve
a problem based directly on problem statement.o Not particularly clever or efficiento Usually intellectually easy to applyo Use the force of the computero Still an important algorithm design strategy
• Example Brute force sorts:o Selection sorto Bubble sorto Insertion sort
Quicksort
Divide and Conquer Divide and Conquer ––Algorithm Design Algorithm Design
StrategyStrategy
• Divide and Conquer- o Divide - A problem is divided into smaller instances of the same
problem, ideally the same size.• Smaller instances are solved(typically using recursion)• AKA recur
o Conquer – Solutions to smaller instances of problems are combined to obtain a solution to original problem.
• Example Divide and Conquer sorts:o Merge sorto Quick sort
Quicksort
Outline Outline • Quick-sort
o Algorithmo Partition stepo Quick-sort treeo Execution example
• Average case intuition
Quicksort
Quick-SortQuick-Sort• Randomized Quick-sort
sorting algorithm is based on the divide-and-conquer paradigm:o Divide: pick a random
element x (called pivot) and partition S into • L elements less than x• E elements equal x• G elements greater than x
o Recur: sort L and Go Conquer: join L, E and G
Quicksort
x
x
L GE
x
Quick-Sort TreeQuick-Sort Tree• An execution of quick-sort is depicted by a binary tree
o Each node represents a recursive call of quicksort and stores• Unsorted sequence before the execution and its pivot• Sorted sequence at the end of the execution
o The root is the initial call o The leaves are calls on subsequences of size 0 or 1
Quicksort
7 4 9 6 2 2 4 6 7 9
4 2 2 4 7 9 7 9
2 2 9 9
Execution ExampleExecution Example• Pivot selection
Quicksort
7 2 9 4 2 4 7 9
2 2
7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9
3 8 6 1 1 3 8 6
3 3 8 89 4 4 9
9 9 4 4
Execution Example Execution Example (cont.)(cont.)
• Partition, recursive call, pivot selection
Quicksort
2 4 3 1 2 4 7 9
9 4 4 9
9 9 4 4
7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9
3 8 6 1 1 3 8 6
3 3 8 82 2
Execution Example Execution Example (cont.)(cont.)
• Partition, recursive call, base case
Quicksort
2 4 3 1 2 4 7
1 1 9 4 4 9
9 9 4 4
7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9
3 8 6 1 1 3 8 6
3 3 8 8
Execution Example Execution Example (cont.)(cont.)
• Recursive call, …, base case, join
Quicksort
3 8 6 1 1 3 8 6
3 3 8 8
7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9
2 4 3 1 1 2 3 4
1 1 4 3 3 4
9 9 4 4
Execution Example Execution Example (cont.)(cont.)
• Recursive call, pivot selection
Quicksort
7 9 7 1 1 3 8 6
8 8
7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9
2 4 3 1 1 2 3 4
1 1 4 3 3 4
9 9 4 4
9 9
2 4 3 1 1 2 3 4
Execution Example Execution Example (cont.)(cont.)
• Partition, …, recursive call, base case
Quicksort
7 9 7 1 1 3 8 6
8 8
7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9
2 4 3 1 1 2 3 4
1 1 4 3 3 4
9 9 4 4
9 9
Execution Example Execution Example (cont.)(cont.)
• Join, join
Quicksort
7 9 7 17 7 9
8 8
7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
2 4 3 1 1 2 3 4
1 1 4 3 3 4
9 9 4 4
9 9
Worst-case Running Worst-case Running TimeTime
• The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element
• Either L and G has size n 1 and the other has size 0• The running time is proportional to the sum
n (n 1) … 2 • Thus, the worst-case running time of quick-sort is O(n2)
Quicksort
depth time
0 n
1 n 1
… …
n 1 1
…
Expected Running Expected Running TimeTime
• Consider a recursive call of quick-sort on a sequence of size so Good call: the sizes of L and G are each less than 3s4o Bad call: one of L and G has size greater than 3s4
• A call is good with probability 12o 1/2 of the possible pivots cause good calls:
Quicksort
7 9 7 1 1
7 2 9 4 3 7 6 1 9
2 4 3 1 7 2 9 4 3 7 61
7 2 9 4 3 7 6 1
Good call Bad call
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Good pivotsBad pivots Bad pivots
Quicksort Quicksort ImplementationImplementation
void quickSort( vector<Item>& v, int a, int b ) {
// Let m be a guess as to the index of the median of the
// items in v in the range v[a] ... v[b]
int M = v[m];
swap( v[m], v[b] )
int L = a;
int R = b-1;
while ( L <= R ) {
while ( v[L] < M ) ++L;
while ( v[R] > M ) --R;
if ( L < R ) swap( v[L], v[R] );
}
swap ( v[b], v[R] ); // putting M in between
quickSort( v, a, L );
quickSort( v, R+1, b );
}
Quicksort