![Page 1: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/1.jpg)
CSE 1302
Lecture 22
Quick Sort and
Merge Sort
Richard Gesick
![Page 2: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/2.jpg)
Merge Sort• Sorts an array by
– Cutting the array in half – Recursively sorting each half – Merging the sorted halves
• Dramatically faster than the selection sort
![Page 3: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/3.jpg)
Merge Sort
![Page 4: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/4.jpg)
The key part of the MergeSortpublic void sort() { if (a.length <= 1) return; int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; System.Array.Copy(a, 0, first, 0, first.length); System.Array.Copy(a, first.length, second, 0,
second.length); MergeSorter firstSorter = new MergeSorter(first); MergeSorter secondSorter = new MergeSorter(second);
firstSorter.sort(); secondSorter.sort(); merge(first, second); }
![Page 5: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/5.jpg)
Merge Sort Vs Selection Sort • Selection sort is an O( n2 ) algorithm• Merge sort is an O( nlog(n) ) algorithm• The nlog(n) function grows much more
slowly than n2
![Page 6: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/6.jpg)
Merge Sort Example• Divide an array in half and sort each half, just working
with the left half for now
![Page 7: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/7.jpg)
Merge Sort Example• Then merge the sorted arrays
7
![Page 8: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/8.jpg)
Merge Sort Example• Then the final Merge of the two sorted arrays into a single
sorted array
![Page 9: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/9.jpg)
The Sortpublic void sort () {
if (a.Length <= 1)
return;
int[] first = new int[a.Length / 2];
int[] second = new int[a.Length - first.Length];
System.Array.Copy (a, 0, first, 0, first.Length);
System.Array.Copy (a, first.Length, second, 0, second.Length);
MergeSorter firstSorter = new MergeSorter (first);
MergeSorter secondSorter = new MergeSorter (second);
firstSorter.sort ();
secondSorter.sort ();
merge (first, second);
}
![Page 10: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/10.jpg)
The Merge
private void merge (int [] first, int [] second)
{
int iFirst = 0; // next element to consider in the first range
int iSecond = 0; // next element to consider in the second range
int j = 0; // next open position in a
// as long as neither iFirst nor iSecond past the end, move
// the smaller element into temp
10
![Page 11: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/11.jpg)
The Mergewhile (iFirst < first.Length && iSecond < second.Length) {
if (first [iFirst] < second [iSecond]) {
a [j] = first [iFirst];
iFirst++;
} else {
a [j] = second [iSecond];
iSecond++;
}
j++;
}
System.Array.Copy (first, iFirst, a, j, first.Length - iFirst);
System.Array.Copy (second, iSecond, a, j, second.Length - iSecond);
}
11
![Page 12: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/12.jpg)
Analyzing the Merge Sort Algorithm
nMerge Sort
(milliseconds)Selection Sort (milliseconds)
10,000 31 772
20,000 47 3,051
30,000 62 6,846
40,000 80 12,188
50,000 97 19,015
60,000 113 27,359
![Page 13: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/13.jpg)
Merge Sort Timing vs. Selection Sort
Figure 2:Merge Sort Timing (blue) versus Selection Sort (red)
![Page 14: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/14.jpg)
Merge Sort Vs Selection Sort• Selection sort is an O(n2) algorithm • Merge sort is an O(nlog(n)) algorithm • The nlog(n) function grows much more
slowly than n2
![Page 15: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/15.jpg)
The Quick Sort• the quick sort is one of the fastest
sorting processes available.• it uses a recursive, divide and conquer
strategy.
![Page 16: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/16.jpg)
the theory• the basic idea is to divide the list into
two parts, based upon a point called the pivot, which is in the middle of the list{(index of first + index of last) / 2}
• at the end of the process, one part will contain all the elements less than the pivot and the other part will contain all the elements larger than the pivot.
![Page 17: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/17.jpg)
the process• array a has 11 elements a[0] to a[10]
– 14 3 2 11 5 8 0 2 9 4 20– 8 is the pivot – we assign a pointer called left arrow to a[0],
the smallest index– and a pointer called right arrow to a[10], the
largest index
![Page 18: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/18.jpg)
more process• left pivot
right• 14 3 2 11 5 8 0 2 9 4 20• now we will start moving our arrows until we
find values that should be exchanged.• starting with the right arrow, it is moved until
we find a value less than or equal to the pivot. Then we move the left arrow until we find a value greater than or equal to the pivot. Once this occurs, we exchange values.
![Page 19: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/19.jpg)
even more process (a)• pivot• 14 3 2 11 5 8 0 2 9 4 20• left right• exchange the values and our new array is• pivot• 4 3 2 11 5 8 0 2 9 14 20• left right • now start moving the arrows again• pivot• 4 3 2 11 5 8 0 2 9 14 20• left right
![Page 20: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/20.jpg)
even more process (b)• left pivot right• 4 3 2 2 5 8 0 11 9 14 20• now the process will stop when the left arrow> right
arrow• since it’s not true yet, continue on• pivot• 4 3 2 2 5 8 0 11 9 14 20• left right• the left arrow stops on 8(the pivot) because we are
looking for values greater than or equal to the pivot and now we swap again
![Page 21: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/21.jpg)
even more process (c)• pivot • 4 3 2 2 5 0 8 11 9 14 20• left right• left arrow is still less than right so continue the
process• pivot • 4 3 2 2 5 0 8 11 9 14 20• right left• the first subdivision is now complete and the 2
sublists can now be sorted using the same function
![Page 22: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/22.jpg)
key parts of the QuickSort • public void quicksort( list_type list, int left, int right)• {• left_arrow=left; • right_arrow=right;• pivot=list[(left+right)/2];• //. . . the recursive calls• if (left<right_arrow)
quicksort(list, left, right_arrow);• if (left_arrow<right)
quicksort(list, left_arrow,right);
![Page 23: CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick](https://reader034.vdocument.in/reader034/viewer/2022050714/56649d215503460f949f6053/html5/thumbnails/23.jpg)
QuickSort Analysis• like the merge sort, the quick sort ends
up with a Big O of O(n log n)