Merge and Quick Sort

advertisement
Lecture 22
Quick Sort and
Merge Sort
Richard Gesick
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
Merge Sort
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
Merge Sort Example
• Divide an array in half and sort each half, just working with
the left half for now
Merge Sort Example
• Then merge the sorted arrays
6
Merge Sort Example
• Then the final Merge of the two sorted arrays into a single
sorted array
The Sort
public 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);
}
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
9
The Merge
while (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);
}
10
Analyzing the Merge Sort Algorithm
n
Merge 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
Merge Sort Timing vs. Selection Sort
Figure 2:
Merge Sort Timing (blue) versus
Selection Sort (red)
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
The Quick Sort
• the quick sort is one of the fastest sorting
processes available.
• it uses a recursive, divide and conquer
strategy.
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.
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
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.
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
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
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
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);
QuickSort Analysis
• like the merge sort, the quick sort ends up
with a Big O of O(n log n)
Download