quick sort and merge sort - the chinese university of hong ... · quick sort •efficient sorting...
TRANSCRIPT
![Page 1: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/1.jpg)
Quick Sort and Merge Sort
Chan Hou Pong, Ken
CSCI2100A Data Structures
1
![Page 2: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/2.jpg)
Quick Sort
• Efficient sorting algorithm
• Example of Divide and Conquer algorithm
• Two phases
– Partition phase
• Divides the work into half
– Sort phase
• Conquers the halves!
2
![Page 3: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/3.jpg)
Quicksort
• Partition
– Choose a pivot
– Find the position for the pivot so that
• all elements to the left are less/equal
• all elements to the right are greater
3
<= pivot > pivotpivot
![Page 4: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/4.jpg)
Quicksort
• Conquer
– Apply the same algorithm to each half
<= pivot > pivot
pivot<= p’ p’ > p’ <=p” p” > p”
![Page 5: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/5.jpg)
5
![Page 6: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/6.jpg)
6
![Page 7: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/7.jpg)
7
![Page 8: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/8.jpg)
8
![Page 9: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/9.jpg)
9
![Page 10: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/10.jpg)
10
![Page 11: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/11.jpg)
11
![Page 12: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/12.jpg)
12
![Page 13: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/13.jpg)
13
![Page 14: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/14.jpg)
14
![Page 15: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/15.jpg)
15
![Page 16: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/16.jpg)
16
![Page 17: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/17.jpg)
17
![Page 18: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/18.jpg)
18
![Page 19: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/19.jpg)
19
![Page 20: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/20.jpg)
20
![Page 21: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/21.jpg)
21
![Page 22: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/22.jpg)
Quicksort
• Implementation
22
quicksort( void *a, int low, int high )
{
int pivot;
/* Termination condition! */
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
}
Divide
Conquer
![Page 23: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/23.jpg)
Quicksort - Partition
23
int partition( int *a, int low, int high ) {
int left, right;
int pivot_item;
pivot_item = a[low];
pivot = left = low;
right = high;
while ( left < right ) {
/* Move left while item < pivot */
while( a[left] <= pivot_item && left < right ) left++;
/* Move right while item > pivot */
while( a[right] > pivot_item) right--;
if ( left < right ) {
SWAP(a,left,right);
}
}
/* right is final position for the pivot */
a[low] = a[right];
a[right] = pivot_item;
return right;
}
![Page 24: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/24.jpg)
Quicksort - Analysis
24
![Page 25: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/25.jpg)
Questions in previous exam
• 7, 6, 1, 4, 8, 2, 3, 5
• Demonstrate ONLY the process of partition the sequence of the QuickSort, with a cutoff of 3
• Choose pivot as the median of the first, middle, and last element of the array
• Please swap the selected pivot with the first element
• Suppose: Size of array is n, middle element index is n/2. Index starts from 0 to n-1
25
![Page 26: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/26.jpg)
Merge Sort - Definition
• Definition:
• A sort algorithm that
• Splits the items to be sorted into twogroups
• Recursively sorts each group
• Merge them into a final sorted sequence.
26
![Page 27: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/27.jpg)
Merge Sort – Divide-and-Conquer
Divide-and-conquer is a general algorithm design paradigm:◦ Divide: divide the input data S in two disjoint
subsets S1 and S2
◦ Conquer: solve the sub-problems associated with S1 and S2 recursively
◦ Combine: combine the solutions for S1 and S2 into a solution for S
The base case for the recursion are sub-problems of size 0 or 1
27
![Page 28: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/28.jpg)
Merge Sort Tree
• An execution of merge-sort is depicted by a binary tree
– each node represents a recursive call of merge-sort, and it stores• unsorted sequence before the execution
• sorted sequence at the end of the execution
– the root is the initial call
– the leaves are calls on subsequences of size 0 or 1
28
![Page 29: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/29.jpg)
Merge Sort - example
29
7 2 9 4 | 3 8 6 1
Partition
![Page 30: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/30.jpg)
30
7 2 9 4 | 3 8 6 1
7 2 | 9 4
Recursive call, partition
Merge Sort - example
![Page 31: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/31.jpg)
31
7 2 9 4 | 3 8 6 1
7 2 | 9 4
7 | 2
Recursive call, partition
Merge Sort - example
![Page 32: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/32.jpg)
32
7 2 9 4 | 3 8 6 1
7 2 | 9 4
7 | 2
77
Recursive call, base case
Merge Sort - example
![Page 33: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/33.jpg)
33
7 2 9 4 | 3 8 6 1
7 2 | 9 4
7 | 2
77 22
Recursive call, base case
Merge Sort - example
![Page 34: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/34.jpg)
34
7 2 9 4 | 3 8 6 1
7 2 | 9 4
7|2 2 7
77 22
Merge
Merge Sort - example
![Page 35: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/35.jpg)
35
7 2 9 4 | 3 8 6 1
7 2 | 9 4
7|2 2 7 9|4 4 9
77 22
Recursive call, base case, …, base case, merge
99 44
Merge Sort - example
![Page 36: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/36.jpg)
36
7 2 9 4 | 3 8 6 1
72|94 2 4 7 9
7|2 2 7 9|4 4 9
77 22 99 44
Merge
Merge Sort - example
![Page 37: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/37.jpg)
37
6 1 1 6
7 2 9 4 | 3 8 6 1
72|94 2 4 7 9 3 8 6 11 3 8 6
7|2 2 7 9|4 4 9
77 22 99 44
3 8 3 8
33 88 66 11
Recursive call, …, merge, merge
Merge Sort - example
![Page 38: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/38.jpg)
38
6 1 1 6
7294|3861 12346789
72|94 2 4 7 9 3 8 6 11 3 8 6
7|2 2 7 9|4 4 9
77 22 99 44
3 8 3 8
33 88 66 11
Merge
Merge Sort - example
![Page 39: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/39.jpg)
Merge Sort - analysis
39
![Page 40: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/40.jpg)
Merge Sort – sample codevoid mergesort(int low, int high){
if (low<high) {
int middle=(low+high)/2; mergesort(low, middle); mergesort(middle+1, high); merge(low, middle, high);
}}
40
![Page 41: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/41.jpg)
Merge Sort – sample code (cont.)void merge(int low, int middle, int high) {
int i, j, k; // copy both halves of a to auxiliary array b for (i=low; i<=high; i++)
b[i]=a[i]; i=low; j=middle+1; k=low; // copy back next-greatest element at each time while (i<=middle && j<=high)
if (b[i]<=b[j]) a[k++]=b[i++]; else a[k++]=b[j++];
// copy back remaining elements of first half (if any) while (i<=middle)
a[k++]=b[i++]; } 41
![Page 42: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/42.jpg)
Questions in previous exam
• Given the sequence (5,2,8,4,1,6,3,7), sort the sequence using Mergesort.
• Illustrate the result after each pass.
• How many comparisons have you performed?
42
![Page 43: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/43.jpg)
Questions in previous exam
• Show all the inversion pairs in the list (4,1,5,2,3)?
• List them out in an organized manner
• i.e., sort them in an ascending order by the first element and then by the second element
43
![Page 44: Quick Sort and Merge Sort - The Chinese University of Hong ... · Quick Sort •Efficient sorting algorithm •Example of Divide and Conquer algorithm •Two phases –Partition phase](https://reader030.vdocuments.net/reader030/viewer/2022040217/5d63f7e588c993ba628b7e20/html5/thumbnails/44.jpg)
Questions in previous exam
• Given an array with n elements to sort,
• Write down the best and worst time complexity when using Bubble sort, Selection sort, Insertion sort and Merge sort in terms of Big-O notation
44