• date post

19-Dec-2015
• Category

## Documents

• view

225

0

TAGS:

Embed Size (px)

### Transcript of Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort...

• Slide 1
• Faster Sorting Methods Chapter 9
• Slide 2
• 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java Class Library Quick Sort The Efficiency of Quick Sort Creating the Partition Quick Sort in the Java Class Library Radix Sort The Efficiency of Quick Sort Comparing the Algorithms
• Slide 3
• 3 Merge Sort Divide an array into halves Sort the two halves Merge them into one sorted array Referred to as a divide and conquer algorithm This is often part of a recursive algorithm However recursion is not a requirement
• Slide 4
• 4 Merge Sort Merging two sorted arrays into one sorted array.
• Slide 5
• 5 Merge Sort The major steps in a merge sort.
• Slide 6
• 6 Merge Sort Algorithm mergeSort(a, first, last) // Sorts the array elements a[first] through a[last] recursively. if (first < last) { mid = (first + last)/2 mergeSort(a, first, mid) mergeSort(a, mid+1, last) Merge the sorted halves a[first..mid] and a[mid+1..last] }
• Slide 7
• 7 Merge Sort The effect of the recursive calls and the merges during a merge sort.
• Slide 8
• 8 Merge Sort Recursive Division step: Assume that n is a power of 2, so we can divide n by 2 evenly. Initial call divide into two subarrays of n/2 elements each. Second call divide into four subarrays of n/2^2 element each Third call divide array into eight subarrays of n/2^3 elements each. If n= 8 = 2^3, it takes three level of recursive call to obtain subarrays of one element each. So total k level of recursive calls result in k levels of merges. K = log 2 n
• Slide 9
• 9 Merge Sort Merge step: Need at most n-1 comparisons among the n elements in the two subarrays; n moves to temporary array; n moves back to original array; In total, each merge requires at most 3n-1 2, 6 4,8 2,4,6,8 1)24, copy 4 to new array 3) 6