110 likes | 269 Views
Lecture 18. Chapter 9. Fast sorting algorithms. Look at two recursive sorting algorithms MergeSort and QuickSort Investigates complexity of each. Some efficient sorting algorithms. Mergesort Quicksort. Both. - very efficient (O(nlogn)). - use recursion.
E N D
Lecture 18 Chapter 9. Fast sorting algorithms Look at two recursive sorting algorithms MergeSort and QuickSort Investigates complexity of each
Some efficient sorting algorithms • Mergesort • Quicksort • Both - very efficient (O(nlogn)) - use recursion - examples of a “divide and conquer” strategy In practice - quicksort is faster - but mergesort easier to implement
Mergesort Fundemental operation: merging two sorted lists (arrays) a and b: Three arrays: a, b and (output array) c and three counters, aptr, bptr and cptr, initially set to beginning of arrays a,b,c. At each stage copy the smaller of a(aptr), b(bptr) to c(cptr) and advance appropriate counters. When either of a or b are exhausted, add remainder of other list to c. Example, to merge lists 1,13,24,26 and 1,15,27,38 Note, aptr is just a counter, not an object reference! And list here just means array
13 13 13 13 24 24 24 24 2 2 2 2 38 38 38 38 1 1 1 1 26 26 26 26 15 15 15 15 27 27 27 27 aptr cptr bptr 1 aptr cptr bptr 1 2 aptr cptr bptr 1 13 2 aptr cptr bptr initially:
1 13 2 15 13 13 13 13 24 24 24 24 2 2 2 2 38 38 38 38 1 1 1 1 26 26 26 26 15 15 15 15 27 27 27 27 aptr cptr bptr 1 13 2 15 24 cptr aptr bptr 1 13 2 15 24 26 cptr aptr bptr 1 13 38 2 15 27 24 26 cptr aptr bptr Remainder of b then copied to c
Mergesort Algorithm Algorithm MergeSort(A,x,y): Input: An integer array A and integer x,y0, such that A has indices x .. y Output: A sorted if x=y then return A else c (x+y)/2 MergeSort(A,x,c) MergeSort(A,c+1,y) Merge(A,c+1) return A Explanation, see board
Merge • See Java code in Sorting subfolder of CodeFromLectures folder • merged array kept in temporary array then copied back into A • Inefficient (space-wise) but easier to implement • More efficient to just rejig A – and use recursion • Try this implementation yourself (part of Lab 5). See board
Examples • Use mergesort to sort 9 8 -3 1 7 6 • See board • Use mergesort to sort 7, 1, 15, 8, 9, 12, -3, 1, 6, -3, • Example for you (now) • What would happen if I asked you to use mergesort to sort 1,1,1,1,1,1,1,1,1,1,1,1,…..,1?
Complexity of Mergesort • Time to merge two sorted lists is linear • At most n-1 comparisons are made, where n is total no. of elements • For n=1, time is constant, denote by 1 • Else Time to mergesort list of n numbers = time to mergesort 2 lists of size n/2 plus time to do merge (which is linear) • Assume n is a power of 2 T(1)=1 T(n)=2T(n/2)+n Example of a recurrence relation. Can be solved in several ways..
Mergesort complexity contd. T(1)=1 T(n)=2T(n/2)+n (1) Will see two methods to solve this Method 1: divide by n, substitute n/2, n/4, etc in for n, and sum. see board Method 2: Continually substitute the recurrence relation on right hand side of (1) T(n)= O (n log n)
(Bits of) exam question From 2006 paper: 2 (a) Describe the mergesort algorithm for sorting a list of integers contained in an array. Illustrate your description by sorting the following list: 2, 1, 3, 9, 4, 8 ,2 [4] (b) Prove that the mergesort algorithm has time complexity O(nlogn). You may assume that the time taken to merge two lists is linear. [5]