Merge Sort. Stable vs. Non-Stable Sorts We frequently use sorting methods for items with multiple keys Sometimes we need to apply the sorting with different.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Chapter 8 CSCI 3333 Data Structures.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
Introduction to Algorithms Chapter 7: Quick Sort.
Sorting Algorithms and Average Case Time Complexity
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Quicksort.
CSCD 326 Data Structures I Sorting
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Some emerging ‘ism’s of the new economyemerging ‘ism’s.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
(c) , University of Washington
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Measuring the Efficiency of Algorithms Analysis of algorithms Provides tools for contrasting the efficiency of different methods of solution Time efficiency,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Sorting Algorithms 2. Quicksort General Quicksort Algorithm: Select an element from the array to be the pivot Select an element from the array to be the.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
CENG 213 Data Structures Sorting Algorithms. CENG 213 Data Structures Sorting Sorting is a process that organizes a collection of data into either ascending.
Sorting: Advanced Techniques Smt Genap
1 CSC MERGESORT –Radix and Bin Sort - Csc 2053 SORTING.
CENG 213 Data Structures Sorting Algorithms. CENG 213 Data Structures Sorting Sorting is a process that organizes a collection of data into either ascending.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
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
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Sorting Algorithms CENG 213 Data Structures 1.
Quick Sort and Merge Sort
Algorithm Design Methods
Chapter 4: Divide and Conquer
Advanced Sorting Methods: Shellsort
Hassan Khosravi / Geoffrey Tien
Yan Shi CS/SE 2630 Lecture Notes
Chapter 4.
CSE 332: Data Abstractions Sorting I
Algorithms Dr. Youn-Hee Han April-May 2013
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
Divide and Conquer Merge sort and quick sort Binary search
Advanced Sorting Methods: Shellsort
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Merge Sort

Stable vs. Non-Stable Sorts We frequently use sorting methods for items with multiple keys Sometimes we need to apply the sorting with different keys For instance we want to sort a list of people based on first name and than on age So Black age 30 should appear before Jones age 30 If we sort a list based on the first key (name) and then apply a sort based on the second key (age) how can we guarantee that the list is still ordered based on the first key? Definition: A sorting method is said the be stable if it preserves the relative order of the items with duplicated keys on the list

An Example of a Stable Sort Adams (30) Washington (23) Wilson (50) Black (23) Brown (40) Smith (30) Thompson (40) Jackson (23) White (50) Jones (50) Adams (30) Washington (23) Wilson (50) Black (23) Brown (40) Smith (30) Thompson (40) Jackson (23) White (50) Jones (50) Adams (30) Black (23) Brown (40) Jackson (23) Jones (50) Smith (30) Thompson (40) Washington (23) White (50) Wilson (50) Adams (30) Black (23) Brown (40) Jackson (23) Jones (50) Smith (30) Thompson (40) Washington (23) White (50) Wilson (50) Black (23) Jackson (23) Washington (23) Adams (30) Smith (30) Brown (40) Thompson (40) Jones (50) White (50) Wilson (50) Black (23) Jackson (23) Washington (23) Adams (30) Smith (30) Brown (40) Thompson (40) Jones (50) White (50) Wilson (50)

Stable vs. Non-Stable Sorts Mergesort is relatively easy to be made stable Just make sure the merge function is stable Another algorithms that sort in O(n log n) is heapsort but it is not stable Quicksort is also not stable You need to understand which of the main sorting algorithms are stable and which ones are not.

Mergesort Quicksort is based on the idea of selecting an element and dividing the list in two halves and then sorting the halves separately We examine the complementary process which is called merging. Given two lists which are ordered combine them into a larger ordered list Selection and merging are complementary because Selection divides a list into two independent lists Merging joins two independent lists into one larger list This should give a hint that Mergesort should consist of two recursive calls and one merging procedure

Mergesort The desirable features of Mergesort It performs in O (n log n) in the worst case It is stable It is quite independent of the way the initial list is organized Good for linked lists. Can me implemented in such a way that data is accessed sequentially Drawbacks It may require an array of up to the size of the original list This can be avoided but the algorithms becomes significantly more complicated making it not worth while Instead of making it complicated we can use heapsort which is also O(n log n)

Understanding the Algorithm 1. Calculate the index of the middle of the list, called it m 2. Use recursion to sort the two partitions [first,m] and [m+1,last] 3. Merge the two ordered lists into one large list 1. Calculate the index of the middle of the list, called it m 2. Use recursion to sort the two partitions [first,m] and [m+1,last] 3. Merge the two ordered lists into one large list

Mergesort in C++ void mergesort(int theArray[], int first, int last) { // PRE: list is an array && // the portion to be sorted runs from first to last inclusive if (first >= last) // Nothing to sort return; int mid = (first+last)/2; // calculate the middle of the list // Recursively call the two partitions mergesort(theArray,first,mid); mergesort(theArray,mid+1,last); merge(theArrayt,first,mid,last); // POST: list is sorted in ascending order between the first // and the last } void mergesort(int theArray[], int first, int last) { // PRE: list is an array && // the portion to be sorted runs from first to last inclusive if (first >= last) // Nothing to sort return; int mid = (first+last)/2; // calculate the middle of the list // Recursively call the two partitions mergesort(theArray,first,mid); mergesort(theArray,mid+1,last); merge(theArrayt,first,mid,last); // POST: list is sorted in ascending order between the first // and the last }

MergeSort Process The list is divided into two equal ( as equal as possible) part. There are different ways to divide the list into two equal part. The following algorithm divides the list until the list has just one item which are sorted Then with recursive merge function calls these smaller pieces merge. Let’s see the algorithm in action.

Merge Sort in Action

On the performance of Mergesort Unlike quicksort, mergesort guarantees O(n log n) in the worst case The reason for this is that quicksort depends on the value of the pivot whereas mergesort divides the list based on the index Why is it O (n log n)? Each merge will require N comparisons Each time the list is halved So the standard divide-and-conquer recurrence applies to mergesort T(n) = 2T * n/2 + (n)

Merge Sort Workhorse…Merge Function void merge(int theArray[], int first, int last) { { tempArray[MAX_SIZE]; int first1 = first; int last1 = mid; int first2 = mid+1; int last2 = last; //while both arrays are not empty, copy the smaller item into the temp array int index = first1; //next available location in tempArray for (; (first1 <= last1 && (first2 <= last2); ++index){ if (theArray[first1],theArray[first2]){ tempArray[index] = theArray[first1]; ++first1; } else{ tempArray[index] = the Array[first2]; ++first2; } //finish off the first sub array, if necessary for (; first1 <= last1; ++first1; ++index) tempArray[index] = theArray[first1]; //finish off the first sub array, if necessary for (; first2 <= last2; ++first2; ++index) tempArray[index] = theArray[first2]; //copy result back into the original array for (index = first; index<= last; ++index) theArray[index] = tempArray[index]; } //end merge void merge(int theArray[], int first, int last) { { tempArray[MAX_SIZE]; int first1 = first; int last1 = mid; int first2 = mid+1; int last2 = last; //while both arrays are not empty, copy the smaller item into the temp array int index = first1; //next available location in tempArray for (; (first1 <= last1 && (first2 <= last2); ++index){ if (theArray[first1],theArray[first2]){ tempArray[index] = theArray[first1]; ++first1; } else{ tempArray[index] = the Array[first2]; ++first2; } //finish off the first sub array, if necessary for (; first1 <= last1; ++first1; ++index) tempArray[index] = theArray[first1]; //finish off the first sub array, if necessary for (; first2 <= last2; ++first2; ++index) tempArray[index] = theArray[first2]; //copy result back into the original array for (index = first; index<= last; ++index) theArray[index] = tempArray[index]; } //end merge