MergeSort01 - Merge Sort In CS1 you probably spent a fair...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Merge Sort In CS1, you probably spent a fair amount of time on several straightforward sorting algorithms such as Insertion Sort and Bubble sort. In both of these algorithms, we end up making a significant number of possible comparisons and swaps between elements. Perhaps it's possible to use information from certain comparisons to reduce the number of future comparisons. So, one might ask if there is a more clever, quicker way to sort numbers that does not require looking at most possible pair of numbers. (Perhaps we can gather extra information from a comparison that renders other comparison’s that could have been done useless.) In this class we will utilize the concept of recursion to come up with a couple more efficient algorithms. One of the more clever sorting algorithms is merge sort. Merge sort utilizes recursion and a clever idea in sorting two separately sorted arrays.
Background image of page 1

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

View Full DocumentRight Arrow Icon
The Merge The merging problem is one that is more simple than sorting an unsorted array, and one that will be a tool we can use in Merge Sort. The problem is that you are given two arrays, each of which is already sorted. Now, your job is to efficiently combine the two arrays into one larger one which contains all of the values of the two smaller arrays in sorted order. The essential idea is this: 1) Keep track of the smallest value in each array that hasn’t been placed in order in the larger array yet. 2) Compare these two smallest values from each array. One of these must be the smallest of all the values in both arrays that are left. Placed the smallest of the two values in the next location in the larger array. 3) Adjust the smallest value for the appropriate array. 4) Continue this process until all values have been placed in the large array. This should look amazingly similar to the Sorted List Matching
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/09/2009.

Page1 / 9

MergeSort01 - Merge Sort In CS1 you probably spent a fair...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online