COP3502_27_MergeSort

COP3502_27_MergeSort - Sorting: Merge Sort Computer Science...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Sorting: Merge Sort COP 3502 – Computer Science I
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sorting: Merge Sort page 2 Sorting: Merge Sort Problem with Bubble/Insertion/Selection Sorts: All of these sorts make a large number of comparisons and swaps between elements As mentioned last class (while covering n 2 sorts): Any algorithm that swaps adjacent elements can only run so fast So one might ask is there a more clever way to sort numbers A way that does not require looking at all these pairs Indeed, there are several ways to do this And one of them is Merge Sort
Background image of page 2
Sorting: Merge Sort page 3 Sorting: Merge Sort Merge Sort Conceptually, Merge Sort works as follows: If the “list” is of length 0 or 1, then it is already sorted! Otherwise: 1. Divide the unsorted list into two sub-lists of about half the size So if your list has n elements, you will divide that list into two sub-lists, each having approximately n/2 elements: 2. Recursively sort each sub-list by calling recursively calling Merge Sort on the two smaller lists 3. Merge the two sub-lists back into one sorted list This Merge is a function that we look at on its own
Background image of page 3

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

View Full DocumentRight Arrow Icon
Sorting: Merge Sort page 4 Sorting: Merge Sort Merge Sort Basically, given a list: You will split this list into two lists of about half the size Then you recursively call Merge Sort on each list What does that do? Each of these new lists will, individually, be split into two lists of about half the size. So now we have four lists, each about ¼ the size of the original list This keeps happening…the lists keep getting split into smaller and smaller lists Until you get to a list of size 1 or size 0 Then we Merge them into a larger, sorted list
Background image of page 4
Sorting: Merge Sort page 5 Sorting: Merge Sort Merge Sort Incorporates two main ideas to improve its runtime: 1) A small list will take fewer steps to sort than a large list 2) Fewer steps are required to construct a sorted list from two sorted lists than two unsorted lists For example: You only have to traverse each list once if they’re already sorted
Background image of page 5

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

View Full DocumentRight Arrow Icon
Sorting: Merge Sort page 6 Sorting: Merge Sort Merge function The key to Merge Sort: the Merge function Given two sorted lists, Merge them into one sorted list Problem: You are given two arrays, each of which is already sorted Your job is to efficiently combine the two arrays into one larger array The larger array should contain all the values of the two smaller arrays Finally, the larger array should be in sorted order
Background image of page 6
Sorting: Merge Sort page 7 Sorting: Merge Sort Merge function The key to Merge Sort: the Merge function Given two sorted lists, Merge them into one sorted list If you have two lists: X (x 1 <x 2 <…<x m ) and Y (y 1 <y 2 <…<y n ) Merge these into one list: Z (z 1 <z 2 <…<z m+n ) Example: List 1 = {3, 8, 9} and List 2 = {1, 5, 7} Merge(List 1, List 2) = {1, 3, 5, 7, 8, 9}
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 84

COP3502_27_MergeSort - Sorting: Merge Sort Computer Science...

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

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