Quicksort Quicksort 29 9
Brute Force – Algorithm Design Strategy Brute force - A straightforward approach to solve a problem based directly on problem statement. o Not particularly clever or efficient o Usually intellectually easy to apply o Use the force of the computer o Still an important algorithm design strategy Example Brute force sorts: o Selection sort o Bubble sort o Insertion sort Quicksort
Divide and Conquer – Algorithm Design Strategy 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 sort o Quick sort Quicksort
Famous Quote “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." -C.A.R. Hoare, inventor of Quicksort Quicksort
Outline Quick-sort o Algorithm o Partition step o Quick-sort tree o Execution example Average case intuition Quicksort
Quick-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 G o Conquer: join L, E and G Quicksort x x L G E x
Quick-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 29 9
Execution Example Pivot selection Quicksort 38 94 4
Execution Example (cont.) Partition, recursive call, pivot selection Quicksort 94 38 8 2 2
Execution Example (cont.) Partition, recursive call, base case Quicksort 11 94 38 8
Execution Example (cont.) Recursive call, …, base case, join Quicksort 38 11 14 3 94 44 4
Execution Example (cont.) Recursive call, pivot selection Quicksort 11 14 3 94 44
Execution Example (cont.) Partition, …, recursive call, base case Quicksort 11 14 3 94 44 4 9 99 9
Execution Example (cont.) Join, join Quicksort 11 14 3 94 44 4 9 99 9
Worst-case Running Time 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(n 2 ) Quicksort depthtime 0n 1 n 1 …… 1 …
Expected Running Time Consider a recursive call of quick-sort on a sequence of size s o Good call: the sizes of L and G are each less than 3s 4 o Bad call: one of L and G has size greater than 3s 4 A call is good with probability 1 2 o 1/2 of the possible pivots cause good calls: Quicksort Good callBad call Good pivotsBad pivots
Quicksort Implementation void quickSort( vector & 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