Section 10.3a Merge Sort.

Slides:



Advertisements
Similar presentations
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Advertisements

Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
CMPS1371 Introduction to Computing for Engineers SORTING.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
CS 280 Data Structures Professor John Peterson. Project Questions?
CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting Chapter 12 Objectives Upon completion you will be able to:
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Chapter 10 Sorting and Searching Algorithms. Selection Sort If we were handed a list of names on a sheet of paper and asked to put them in alphabetical.
Searching and Sorting Searching algorithms with simple arrays
Merge Sort.
Advanced Sorting.
Sort Algorithm.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Recitation 13 Searching and Sorting.
Algorithm Efficiency and Sorting
CS 162 Intro to Programming II
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Chapter 7 Sorting Spring 14
Section 10.3b Quick Sort.
Divide and Conquer.
Insertion Sort Sorted Unsorted
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
Sorting means The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) Sorting.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Chapter 4: Divide and Conquer
Advanced Sorting Methods: Shellsort
Hassan Khosravi / Geoffrey Tien
MSIS 655 Advanced Business Applications Programming
Sorting Algorithms Ellysa N. Kosinaya.
C++ Plus Data Structures
CSE 373 Data Structures and Algorithms
Yan Shi CS/SE 2630 Lecture Notes
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4.
Algorithm Efficiency and Sorting
CS 1114: Sorting and selection (part two)
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide & Conquer Algorithms
Chapter 11 Sorting and Searching Algorithms
Chapter 10 Sorting Algorithms
Algorithm Efficiency and Sorting
Searching/Sorting/Searching
CSC 380: Design and Analysis of Algorithms
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Divide and Conquer Merge sort and quick sort Binary search
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Section 10.3a Merge Sort

10.3 O(N log2N) Sorts O(N2) sorts are very time consuming for sorting large arrays. Several sorting methods that work better when N is large are presented in this section. The efficiency of these algorithms is achieved at the expense of the simplicity seen in the straight selection, bubble, and insertion sorts.

The Merge Sort The sorting algorithms covered in Section 10.2 are all O(N2). Note that N2 is a lot larger than (½N)2 + (½N)2 = ½N2 If we can cut the array into two pieces, sort each segment, and then merge the two back together, we should end up sorting the entire array with a lot less work.

Rationale for Divide and Conquer

Merge Sort Algorithm mergeSort Cut the array in half Sort the left half Sort the right half Merge the two sorted halves into one sorted array Because mergeSort is itself a sorting algorithm, we might as well use it to sort the two halves. We can make mergeSort a recursive method and let it call itself to sort each of the two subarrays: mergeSort—Recursive mergeSort the left half mergeSort the right half

Merge Sort Summary Method mergeSort(first, last) Definition: Sorts the array elements in ascending order. Size: last - first + 1 Base Case: If size less than 2, do nothing. General Case: Cut the array in half. mergeSort the left half. mergeSort the right half. Merge the sorted halves into one sorted array.

Strategy for merging two sorted arrays

Our actual merge problem

Our solution

The mergeSort operation Concept: recursively divide the array in half until each sub-array has only one element merge each of the two halves back together in order Pseudocode: mergeSort(first, last) if(first < last) middle = first + last / 2 mergesort(first, middle) mergesort(middle + 1, last) merge(first, middle, middle + 1, last)

The merge operation note: uses in-place sorting (different from textbook) Concept: merge in-place two sorted sub-arrays into one sorted array array index leftFirst . . . leftLast is one sorted sub-array, array index rightFirst . . . rightLast is the other sorted sub-array Pseudocode: merge(leftFirst, leftLast, rightFirst, rightLast) if (array[rightFirst ]> array[leftLast]) then return // no need to merge, already in order while(leftFirst <= leftLast AND rightFirst <= rightLast) if(array[rightFirst] > array[leftFirst]) increment leftFirst // select from left; no change, just increment left pointer else for(int i = rightFirst; i > leftFirst; i--) // select from right; rotate[left..right] and correct swap(i, i - 1) increment leftFirst, leftLast, rightFirst // everything has now moved up by one

Analysing Merge Sort

Analyzing Merge Sort The total work needed to divide the array in half, over and over again until we reach subarrays of size 1, is O(N). It takes O(N) total steps to perform merging at each “level” of merging. The number of levels of merging is equal to the number of times we can split the original array in half If the original array is size N, we have log2N levels. Because we have log2N levels, and we require O(N) steps at each level, the total cost of the merge operation is: O(N log2N). Because the splitting phase was only O(N), we conclude that Merge Sort algorithm is O(N log2N).

Comparing N2 and N log2 N N log2N N2 N log2N 32 5 1,024 160 32 5 1,024 160 64 6 4,096 384 128 7 16,384 896 256 8 65,536 2,048 512 9 262,144 4,608 1024 10 1,048,576 10,240 2048 11 4,194,304 22,528 4096 12 16,777,216 49,152

Drawback of Merge Sort mergeSort is most often implemented using a temporary (scratch) storage array, which is often faster than sorting in-place, because fewer swaps take place. A disadvantage of this implementation is that the temporary array needs to be as large as the original array to be sorted. If the array is large and space is a critical factor, this type of sort may not be an appropriate choice.