mergesort-102-sp10

# mergesort-102-sp10 - MergeSort Source: Gibbs & Tamassia...

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

MergeSort Source: Gibbs & Tamassia

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

View Full Document
2 MergeSort MergeSort is a divide and conquer method of sorting
3 MergeSort Algorithm MergeSort is a recursive sorting procedure that uses at most O(n lg(n)) comparisons. To sort an array of n elements, we perform the following steps in sequence: If n < 2 then the array is already sorted. Otherwise, n > 1 , and we perform the following three steps in sequence: 1. Sort the left half of the the array using MergeSort. 2. Sort the right half of the the array using MergeSort. 3. Merge the sorted left and right halves.

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

View Full Document
4 How to Merge Here are two lists to be merged: First: (12, 16, 17, 20, 21, 27) Second: (9, 10, 11, 12, 19) Compare 12 and 9 First: (12, 16, 17, 20, 21, 27) Second: (10, 11, 12, 19) New: (9) Compare 12 and 10 First: (12, 16, 17, 20, 21, 27) Second: (11, 12, 19)
5 Merge Example Compare 12 and 11 First: (12, 16, 17, 20, 21, 27) Second: (12, 19) New: (9, 10, 11) Compare 12 and 12 First: (16, 17, 20, 21, 27) Second: (12, 19) New: (9, 10, 11, 12)

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

View Full Document
6 Merge Example Compare 16 and 12 First: (16, 17, 20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12) Compare 16 and 19 First: (17, 20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12, 16)
7 Merge Example Compare 17 and 19 First: (20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12, 16, 17) Compare 20 and 19 First: (20, 21, 27) Second: ( ) New: (9, 10, 11, 12, 12, 16, 17, 19)

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

View Full Document
8 Merge Example Checkout 20 and empty list First: ( ) Second: ( ) New: (9, 10, 11, 12, 12, 16, 17, 19, 20 , 21 , 27 )
9 MergeSort Original 24 13 26 1 12 27 38 15 Divide in 2 24 13 26 1 12 27 38 15 Divide in 4 24 13 26 1 12 27 38 15 Divide in 8 24 13 26 1 12 27 38 15 Merge 2 13 24 1 26 12 27 15 38 Merge 4 1 13 24 26 12 15 27 38 Merge 8 1 12 13 15 24 26 27 38

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

View Full Document
10 Merge-Sort Tree
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/06/2010 for the course COMPUTER S 101 taught by Professor Sanaodeh during the Spring '08 term at NYU.

### Page1 / 22

mergesort-102-sp10 - MergeSort Source: Gibbs & Tamassia...

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

View Full Document
Ask a homework question - tutors are online