{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

12. MergeSort_outside

2 4 7 9 3 8 6 1 → 1 3 8 6 7 2 → 2 7 9 4 → 4 9 3

Info iconThis preview shows pages 13–17. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 2 4 7 9 3 8 6 1 → 1 3 8 6 7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9 © 2004 Goodrich, Tamassia Merge Sort 14 Execution Example (cont.) Recursive call, …, merge, merge 7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 6 8 7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9 © 2004 Goodrich, Tamassia Merge Sort 15 Execution Example (cont.) Merge 7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 6 8 7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9 © 2004 Goodrich, Tamassia Merge Sort 16 Analysis of Merge-Sort The height h of the merge-sort tree is O (log n ) at each recursive call we divide in half the sequence, The overall amount or work done at the nodes of depth i is O ( n ) we partition and merge 2 i sequences of size n / 2 i we make 2 i + 1 recursive calls Thus, the total running time of merge-sort is O ( n log n ) depth #seqs size 1 n 1 2 n / 2 i 2 i n / 2 i … … … © 2004 Goodrich, Tamassia Merge Sort 17 Summary of Sorting Algorithms Algorithm Time Notes selection-sort O ( n 2 ) slow in-place for small data sets (< 1K) insertion-sort O ( n 2 ) slow in-place for small data sets (< 1K) heap-sort O ( n log n ) fast in-place for large data sets (1K — 1M) merge-sort O ( n log n ) fast sequential data access for huge data sets (> 1M)...
View Full Document

{[ snackBarMessage ]}

Page13 / 17

2 4 7 9 3 8 6 1 → 1 3 8 6 7 2 → 2 7 9 4 → 4 9 3 8 →...

This preview shows document pages 13 - 17. Sign up to view the full document.

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