36
CS 171: Introduction to Computer Science II Mergesort

CS 171: Introduction to Computer Science II Mergesort

Embed Size (px)

Citation preview

CS 171: Introduction to Computer Science II

Mergesort

HW3a

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?

Midterm!

To be held in-class on March 5 Closed-book Covers:

Homeworks HW1-3 Material covered in class Arrays (including binary search) Linked lists Algorithmic complexity (big-O notation) Divide-and-conquer Sorting algorithms

Roadmap

Elementary sorting (quadratic cost) Bubble sort Selection sort Insertion sort

Recursion (review) and analysis Advanced sorting

MergeSort QuickSort

• Basic idea– Divide an array into two halves– Sort each half– Merge the two sorted halves into a sorted array

Mergesort

Mergesort

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)

Mergesort: merging

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

Merging Two Sorted ArraysMerging 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?

Merging

Recursive Mergesort

First base caseencountered

Return, andcontinue

Merge

Merge

Merge

Mergesort demo

Animation http://www.sorting-algorithms.com/merge-sort

German folk dance http://www.youtube.com/watch?v=XaqR3G_NVo

o

Roadmap

MergeSort Recursive Algorithm (top-down) Improvements Non-recursive algorithm (bottom-up)

Runtime analysis of recursive algorithms QuickSort

MergeSort

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

MergeSort Improvements

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

Roadmap

MergeSort Recursive Algorithm (top-down) Improvements Non-recursive algorithm (bottom-up)

Runtime analysis of recursive algorithms QuickSort

Recursive MergeSort (top-down)

Non-Recursive MergeSort (bottom-up)

Roadmap

MergeSort Recursive Algorithm (top-down) Improvements Non-recursive algorithm (bottom-up)

Runtime analysis of recursive algorithms QuickSort

Recursive Mergesort

Merging

Mergesort Cost

Mergesort Recursively sort 2 half-arrays Merge 2 half-arrays into 1 array

Mergesort Cost

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

Mergesort Cost

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)

How long does it take?

Nerd Sort

http://www.smbc-comics.com/?db=comics&id=1989