{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COP3502_27_MergeSort

# COP3502_27_MergeSort - Sorting Merge Sort Computer Science...

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

Computer Science Department University of Central Florida Sorting: Merge Sort COP 3502 – Computer Science I

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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}

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

View Full Document
Sorting: Merge Sort page 8 Sorting:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern