# rec08 - 6.006 Intro to Algorithms Recitation 08 March 2...

This preview shows pages 1–2. Sign up to view the full content.

6.006 Intro to Algorithms Recitation 08 March 2, 2011 Merge Sort The merge sort algorithm deals with the problem of sorting a list of n elements. It is able to sort a list of n elements in O ( n log n ) runtime, which is considerably faster than insertion sort, which takes O ( n 2 ) . Merge sort uses a divide and conquer method: 1. If the length of the list is 1, the list is sorted. Return the list 2. Otherwise, split the list in two (roughly) equal halves and then recursively merge sort the two halves 3. Merge the two sorted halves into one sorted list The merge operation takes two sorted lists and an iterator at the head of each list. At each step, we compare the elements at the iterators with each other. We take the smaller element, add it to our merged list, and then advance the iterator associated with that smaller element. We repeat this step until every element in the two sorted halves has been added to the merged list, forming a single large sorted list. The runtime of the merge operation is O ( n ) where n is the number of elements we are merging. Solving Recurrences We can use merge sort as an example of how to solve recurrences. Recall back to peak ﬁnding where we solved recurrences by showing them in the form of “Runtime of original problem” = “Runtime of reduced problem” + “Time taken to reduce problem”, and then solved them using the dot dot dot method. We are going to formalize this a little more. The form of the recurrences that we’ll be dealing with look like:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

rec08 - 6.006 Intro to Algorithms Recitation 08 March 2...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online