The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Lecture 5 - quicksort Next: Lecture 6 - linear Up: No Title Previous: Lecture 4 - heapsort Lecture 5 - quicksort Listen To Part 5-1 4-2 Find the missing integer from 0 to n using O(n) ``is bit[j] in A[i]'' queries. Note - there are a total of bits, so we are not allowed to read the entire input! Also note, the problem is asking us to minimize the number of bits we read. We can spend as much time as we want doing other things provided we don't look at extra bits. How can we find the last bit of the missing integer? Ask all the n integers what their last bit is and see whether 0 or 1 is the bit which occurs less often than it is supposed to. That is the last bit of the missing integer! How can we determine the second-to-last bit? Ask the numbers which ended with the correct last bit! By analyzing the bit patterns of the numbers from 0 to n which end with this bit. By recurring on the remaining candidate numbers, we get the answer in T(n) = T(n/2) + n =O(n), by the Master Theorem. Listen To Part 5-2 Quicksort Although mergesort is , it is quite inconvenient for implementation with arrays, since we need space to merge. In practice, the fastest sorting algorithm is Quicksort, which uses partitioning as its main idea. Example: Pivot about 10. 17 12 6 19 23 8 5 10 - before 6 8 5 10 23 19 12 17 - after file:///E|/LEC/LECTUR17/NODE5.HTM (1 of 10) [19/1/2003 1:34:35]
Lecture 5 - quicksort Partitioning places all the elements less than the pivot in the left part of the array, and all elements greater than the pivot in the right part of the array. The pivot fits in the slot between them. Note that the pivot element ends up in the correct place in the total order! Listen To Part 5-3 Partitioning the elements Once we have selected a pivot element, we can partition the array in one linear scan, by maintaining three sections of the array: < pivot, > pivot, and unexplored. Example: pivot about 10 | 17 12 6 19 23 8 5 | 10 | 5 12 6 19 23 8 | 17 5 | 12 6 19 23 8 | 17 5 | 8 6 19 23 | 12 17 5 8 | 6 19 23 | 12 17 5 8 6 | 19 23 | 12 17 5 8 6 | 23 | 19 12 17 5 8 6 ||23 19 12 17 5 8 6 10 19 12 17 23 As we scan from left to right, we move the left bound to the right when the element is less than the pivot, otherwise we swap it with the rightmost unexplored element and move the right bound one step closer to the left. Listen To Part 5-4 Since the partitioning step consists of at most n swaps, takes time linear in the number of keys. But what does it buy us? 1. The pivot element ends up in the position it retains in the final sorted order. 2. After a partitioning, no element flops to the other side of the pivot in the final sorted order. file:///E|/LEC/LECTUR17/NODE5.HTM (2 of 10) [19/1/2003 1:34:35]
- Page 835 and 836: Binary Search in Action Binary Sear
- Page 837 and 838: Binary Search in Action file:///E|/
- Page 839 and 840: Binary Search in Action file:///E|/
- Page 841 and 842: Postscript version of the lecture n
- Page 843 and 844: Lecture 1 - analyzing algorithms Ne
- Page 845 and 846: Lecture 1 - analyzing algorithms Yo
- Page 847 and 848: Lecture 1 - analyzing algorithms Th
- Page 849 and 850: Lecture 1 - analyzing algorithms Th
- Page 851 and 852: Lecture 1 - analyzing algorithms is
- Page 853 and 854: Lecture 2 - asymptotic notation How
- Page 855 and 856: Lecture 2 - asymptotic notation Not
- Page 857 and 858: Lecture 2 - asymptotic notation alg
- Page 859 and 860: Lecture 2 - asymptotic notation Sup
- Page 861 and 862: Lecture 2 - asymptotic notation Nex
- Page 863 and 864: Lecture 3 - recurrence relations Is
- Page 865 and 866: Lecture 3 - recurrence relations
- Page 867 and 868: Lecture 3 - recurrence relations Li
- Page 869 and 870: Lecture 3 - recurrence relations Su
- Page 871 and 872: Lecture 3 - recurrence relations A
- Page 873 and 874: Lecture 4 - heapsort Note iteration
- Page 875 and 876: Lecture 4 - heapsort The convex hul
- Page 877 and 878: Lecture 4 - heapsort 1. All leaves
- Page 879 and 880: Lecture 4 - heapsort left = 2i righ
- Page 881 and 882: Lecture 4 - heapsort Since this sum
- Page 883 and 884: Lecture 4 - heapsort Selection sort
- Page 885: Lecture 4 - heapsort Greedy Algorit
- Page 889 and 890: Lecture 5 - quicksort Listen To Par
- Page 891 and 892: Lecture 5 - quicksort Listen To Par
- Page 893 and 894: Lecture 5 - quicksort The worst cas
- Page 895 and 896: Lecture 5 - quicksort Mon Jun 2 09:
- Page 897 and 898: Lecture 6 - linear sorting Since 2i
- Page 899 and 900: Lecture 6 - linear sorting With uni
- Page 901 and 902: Lecture 6 - linear sorting Listen T
- Page 903 and 904: Lecture 7 - elementary data structu
- Page 905 and 906: Lecture 7 - elementary data structu
- Page 907 and 908: Lecture 7 - elementary data structu
- Page 909 and 910: Lecture 7 - elementary data structu
- Page 911 and 912: Lecture 7 - elementary data structu
- Page 913 and 914: Lecture 7 - elementary data structu
- Page 915 and 916: Lecture 7 - elementary data structu
- Page 917 and 918: Lecture 8 - binary trees - Joyce Ki
- Page 919 and 920: Lecture 8 - binary trees TREE-MINIM
- Page 921 and 922: Lecture 8 - binary trees Inorder-Tr
- Page 923 and 924: Lecture 8 - binary trees Listen To
- Page 925 and 926: Lecture 8 - binary trees insert(b)
- Page 927 and 928: Lecture 8 - binary trees Not (1) -
- Page 929 and 930: Lecture 9 - catch up Next: Lecture
- Page 931 and 932: Lecture 10 - tree restructuring 1.
- Page 933 and 934: Lecture 10 - tree restructuring Lis
- Page 935 and 936: Lecture 10 - tree restructuring Not
Lecture 5 - quicksort<br />
Partitioning places all the elements less than the pivot in the left part of the array, and all elements greater than the<br />
pivot in the right part of the array. <strong>The</strong> pivot fits in the slot between them.<br />
Note that the pivot element ends up in the correct place in the total order!<br />
Listen To Part 5-3<br />
Partitioning the elements<br />
Once we have selected a pivot element, we can partition the array in one linear scan, by maintaining three sections<br />
of the array: < pivot, > pivot, and unexplored.<br />
Example: pivot about 10<br />
| 17 12 6 19 23 8 5 | 10<br />
| 5 12 6 19 23 8 | 17<br />
5 | 12 6 19 23 8 | 17<br />
5 | 8 6 19 23 | 12 17<br />
5 8 | 6 19 23 | 12 17<br />
5 8 6 | 19 23 | 12 17<br />
5 8 6 | 23 | 19 12 17<br />
5 8 6 ||23 19 12 17<br />
5 8 6 10 19 12 17 23<br />
As we scan from left to right, we move the left bound to the right when the element is less than the pivot, otherwise<br />
we swap it with the rightmost unexplored element and move the right bound one step closer to the left.<br />
Listen To Part 5-4<br />
Since the partitioning step consists of at most n swaps, takes time linear in the number of keys. But what does it<br />
buy us?<br />
1. <strong>The</strong> pivot element ends up in the position it retains in the final sorted order.<br />
2. After a partitioning, no element flops to the other side of the pivot in the final sorted order.<br />
file:///E|/LEC/LECTUR17/NODE5.HTM (2 of 10) [19/1/2003 1:34:35]