308-203a introduction to computing ii lecture 8: sorting 2 fall session 2000
TRANSCRIPT
![Page 1: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/1.jpg)
308-203AIntroduction to Computing II
Lecture 8: Sorting 2
Fall Session 2000
![Page 2: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/2.jpg)
So far….
1. Bubblesort - intuitive implementation
(n2) suboptimal
2. MergeSort - Divide-and-Conquer
O(n log n) worst-case optimal
![Page 3: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/3.jpg)
The Problem with MergeSort
We do a lot of copying when we MERGE…
8, 34, 51, 78, 82 2, 17, 64, 91, 123
2, 8, 17, 34, 51, 64, 78, 82, 91, 123
N/2 N/2
New Array: size = N
![Page 4: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/4.jpg)
In Java, for example...
int [] merge(int [] a, int [] b){ int [] c = new int [a.length + b.length];
// merge a and b into c ….
return c;}
![Page 5: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/5.jpg)
The Problem with MergeSort
• To merge we need O(n) extra space
• Copying bigger “hidden constants”
• “Sorting-in-place” would be preferable (if we can do it in optimal n log n)
• Divide-and-Conquer was good, can we do the same thing with an operation other than merge??
![Page 6: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/6.jpg)
Quicksort
64, 8, 78, 34, 82, 51, 17, 2, 91, 123
2. Recursion
2. Partition
1. Pick pivot
2, 8, 17, 34, 51, 64, 78, 82, 91, 123
8, 34, 51, 17, 2 , 64, 78, 82, 91, 123
![Page 7: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/7.jpg)
Quicksort
Quicksort(int [] A, int start, int end){ int pivot = A[0];
int midpoint = Partition(pivot, A);
Quicksort(A, start, midpoint); Quicksort(A, midpoint+1, end);}
![Page 8: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/8.jpg)
Running Time
• Depends significantly on choice of pivot
• Worst-case = O(n2)
• Best-case = O( n log n)
Proofs on separate handout
![Page 9: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/9.jpg)
The Worst-Case is WORSE!
• Worst-case is O(n2) as bad as Bubblesort
• A clever trick:Fooling-the-Adversary
1. pick a random pivot2. You hit a bad case only when very unlucky
• Average-case = O( n log n)
Proof beyond the scope of this course
![Page 10: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/10.jpg)
Randomized Quicksort
Quicksort(int [] A, int start, int end){ int pivot = A[ randomInt(start, end) ];
int midpoint = partition(pivot, A);
Quicksort(A, start, midpoint); Quicksort(A, midpoint+1, end);}
![Page 11: 308-203A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000](https://reader036.vdocument.in/reader036/viewer/2022082709/56649d215503460f949f5ff0/html5/thumbnails/11.jpg)
Any questions?