Download presentation
Presentation is loading. Please wait.
Published byMagdalene Dean Modified over 9 years ago
1
CS 171: Introduction to Computer Science II Mergesort
2
We’ll implement our own Deque (doubly- ended queue) ! You’ve been given a singly-linked list implementation But removeLast() is too slow Goal: Implement using an iterable doubly-linked list Due: What would you like?
4
Elementary sorting (quadratic cost) Bubble sort Selection sort Insertion sort Recursion (review) and analysis Advanced sorting MergeSort QuickSort
5
Basic idea – Divide an array into two halves – Sort each half – Merge the two sorted halves into a sorted array
6
A divide and conquer approach using recursion Partition the original problem into two sub-problems Solve each sub-problem using recursion (sort) Combine the results to solve the original problem (merge)
7
Merge Two Sorted Arrays A key step in mergesort Assume subarrays a[lo..mid] (left half) and a[mid+1...hi] (right half) are sorted Copy a[] to an auxiliary array aux[] Merge the two halves of aux[] to a[] ▪ Such that it contains all elements and remains sorted
8
Merging Two Sorted Arrays Start from the first elements of each half Compare and copy the smaller element to a[] Increment index for the array that had the smaller element Continue If we reach the end of either half Copy the remaining elements of the other half What’s the cost of merging N elements?
12
First base case encountered
13
Return, and continue
14
Merge
18
Animation http://www.sorting-algorithms.com/merge-sort http://www.sorting-algorithms.com/merge-sort German folk dance http://www.youtube.com/watch?v=XaqR3G_NVo o http://www.youtube.com/watch?v=XaqR3G_NVo o
19
MergeSort Recursive Algorithm (top-down) Improvements Non-recursive algorithm (bottom-up) Runtime analysis of recursive algorithms QuickSort
20
Recursion overhead for tiny subarrays Merging cost even when the array is already sorted Requires additional memory space for auxiliary array QuickSort will address this later
23
MergeX.java Use insertion sort for small subarrays improve the running time by 10 to 15 percent. Test whether array is already in order reduce merging cost for sorted subarrays Eliminate the copy to the auxiliary array Switches the role of the input array and the auxiliary array at each level ▪ one sorts an input array and puts the sorted output in the auxiliary array ▪ one sorts the auxiliary array and puts the sorted output in the given array
24
MergeSort Recursive Algorithm (top-down) Improvements Non-recursive algorithm (bottom-up) Runtime analysis of recursive algorithms QuickSort
25
Recursive MergeSort (top-down)
26
Non-Recursive MergeSort (bottom-up)
29
MergeSort Recursive Algorithm (top-down) Improvements Non-recursive algorithm (bottom-up) Runtime analysis of recursive algorithms QuickSort
32
Mergesort Recursively sort 2 half-arrays Merge 2 half-arrays into 1 array
33
Mergesort Recursively sort 2 half-arrays Merge 2 half-arrays into 1 array (cost: N) Suppose cost function for sorting N items is T(N) T(N) = 2 * T(N/2) + N T(1) = 0
34
Mergesort Recursively sort 2 half-arrays Merge 2 half-arrays into 1 array (cost: N) Suppose cost function for sorting N items is T(N) T(N) = 2 * T(N/2) + N T(1) = 0 Big O cost: N lg(N)
36
http://www.smbc- comics.com/?db=comics&id=1989 http://www.smbc- comics.com/?db=comics&id=1989
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.