Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quicksort Quicksort 7 4 9 6 2  2 4 6 7 9 4 2  2 47 9  7 9 2  29  9.

Similar presentations


Presentation on theme: "Quicksort Quicksort 7 4 9 6 2  2 4 6 7 9 4 2  2 47 9  7 9 2  29  9."— Presentation transcript:

1 Quicksort Quicksort 7 4 9 6 2  2 4 6 7 9 4 2  2 47 9  7 9 2  29  9

2 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

3 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

4 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

5 Outline Quick-sort o Algorithm o Partition step o Quick-sort tree o Execution example Average case intuition Quicksort

6 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

7 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 7 4 9 6 2  2 4 6 7 9 4 2  2 47 9  7 9 2  29  9

8 Execution 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  38  8 9 4  4 9 9  94  4

9 Execution Example (cont.) Partition, recursive call, pivot selection Quicksort 2 4 3 1  2 4 7 9 9 4  4 9 9  94  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  38  8 2  2

10 Execution Example (cont.) Partition, recursive call, base case Quicksort 2 4 3 1  2 4 7 1  11  1 9 4  4 9 9  94  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  38  8

11 Execution Example (cont.) Recursive call, …, base case, join Quicksort 3 8 6 1  1 3 8 6 3  38  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  11  14 3  3 4 9  94  44  4

12 Execution Example (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  11  14 3  3 4 9  94  44  4 2 4 3 1  1 2 3 4

13 Execution Example (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  11  14 3  3 4 9  94  44  4 9  99  9

14 Execution Example (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  11  14 3  3 4 9  94  44  4 9  99  9

15 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 …

16 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 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 callBad call 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Good pivotsBad pivots

17 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


Download ppt "Quicksort Quicksort 7 4 9 6 2  2 4 6 7 9 4 2  2 47 9  7 9 2  29  9."

Similar presentations


Ads by Google