Sorting - Merge Sort Cmput 115 - Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Garfield AP Computer Science
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Algorithms and Average Case Time Complexity
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.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
1 Algorithm Efficiency and Sorting (Walls & Mirrors - Remainder of Chapter 9)
Spring 2010CS 2251 Sorting Chapter 8. Spring 2010CS 2252 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
C++ Plus Data Structures
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Sorting Chapter 10.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
Section 8.4 Insertion Sort CS Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.
(c) , University of Washington
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
Merge Sort. In plain English: if the size of the array > 1, split the array into two halves, and recursively sort both halves; when the sorts return,
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
Algorithms Lakshmish Ramaswamy. Merge Problem – Merge two sorted arrays such that the resultant array remains sorted Logic –Keep pointers to both arrays.
CS 367 Introduction to Data Structures Lecture 11.
FASTER SORT using RECURSION : MERGE SORT COMP 103.
Prof. U V THETE Dept. of Computer Science YMA
Sorting Mr. Jacobs.
FASTER SORT using RECURSION : MERGE SORT
Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
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.
Advanced Sorting Methods: Shellsort
C++ Plus Data Structures
Yan Shi CS/SE 2630 Lecture Notes
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
CSE 373 Data Structures and Algorithms
Sorting and Asymptotic Complexity
Searching/Sorting/Searching
Advanced Sorting Methods: Shellsort
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 1/26/00

©Duane Szafron About This Lecture In this lecture we will learn about a sorting algorithm called the Merge Sort. We will study its implementation and its time and space complexity.

©Duane Szafron Outline The Merge Sort Algorithm Merge Sort - Arrays Time and Space Complexity of Merge Sort

©Duane Szafron The Sort Problem Given a collection, with elements that can be compared, put the elements in increasing or decreasing order

©Duane Szafron Merge Sort Algorithm - Splitting Split the list into two halves: the bottom part and the top part. Make a recursive call to Merge Sort for each half

©Duane Szafron Merge Sort Algorithm - Merging Merge the bottom sorted half and the top sorted half into a sorted whole using a Merge algorithm

©Duane Szafron Merge Algorithm 0 The Merge algorithm requires temporary storage, the same size as the collection. Before a merge, we place each sub- collection in its own collection

©Duane Szafron Merge Algorithm 1 Copy the smallest from the two collections

©Duane Szafron Merge Algorithm 2 Copy the smallest from the two collections

©Duane Szafron Merge Algorithm 3 Copy the smallest from the two collections

©Duane Szafron Merge Algorithm 4 Copy the smallest from the two collections

©Duane Szafron Merge Algorithm 5 Copy the smallest from the two collections

©Duane Szafron Merge Algorithm 6 Copy the smallest from the two collections

©Duane Szafron Merge Algorithm 7 When one of the two collections is empty: –If the bottom collection is emptied first, then elements remain in the top collection, but they are in the correct place, so stop

©Duane Szafron Merge Algorithm 8 –If the top collection is emptied first, then elements remain in the bottom collection. –For example, if the original list had the 90 and 60 exchanged and the 80 and 30 exchanged, the top collection would be emptied first –All remaining elements must be copied from the bottom collection to the top.

©Duane Szafron Merge Algorithm - Arrays 1 private static void merge(Comparable top[ ], Comparable bottom[ ] int low, int middle, int high) { // pre: top[middle..high] are ascending // bottom[low..middle-1] are ascending // post: top[low..high] are ascending inttopIndex; //index of top array int botIndex; //index of bottom array int resultIndex; //index of result array topIndex = middle; botIndex = low; resultIndex = low; code based on Bailey pg. 85

©Duane Szafron Merge Algorithm - Arrays 2 // as long as both collections are not empty while (bottIndex < middle) && (topIndex <= high) if (top[topIndex].compareTo(bottom[botIndex]) < 0) // copy from the top collection top[resultIndex++] = top[topIndex++]; else // or copy from the bottom collection top[resultIndex++] = bottom[botIndex++]; // copy any remaining elements from the bottom for (botIndex = botIndex; botIndex < middle; botIndex++) top[resultIndex++] = bottom[botIndex++]; } code based on Bailey pg. 85 What about remaining elements in top?

©Duane Szafron MergeSort Algorithm The MergeSort is recursive, so we create a wrapper method and the recursive method that it calls.

©Duane Szafron MergeSort Algorithm - Arrays public static void mergeSort(Comparable anArray[], int size) { // pre: 0 <= size <= anArray.length // post: values in anArray[0..size - 1] are in // ascending order mergeSortR(anArray, new int[size], 0, size - 1); } code based on Bailey pg. 88

©Duane Szafron MergeSortR Algorithm - Arrays public static void mergeSortR(Comparable anArray[ ], Comparable temp[ ], int low, int high) { // pre: 0 <= low <= high <= anArray.length // post: values in anArray[low..high] are in ascending order int size; // size of the entire collection int index; // index for copying bottom into temp size = high - low + 1; if (size < 2) return; middle = (low + high) / 2; for (index = low; index < middle; index++) temp[index] = anArray[index]; mergeSortR(temp, anArray, low, middle - 1); mergeSortR(anArray, temp, middle, high); merge(anArray, temp, low, middle, high); } code based on Bailey pg. 86

©Duane Szafron MergeSort Calling Sequence mSR(a, t, 0, 8) –mSR(t, a, 0, 3) mSR(t, a, 0, 1) –msR(t, a, 0, 0) * –msR(a, t, 1, 1) * –merge(a, t, 0, 1, 1) mSR(a, t, 2, 3) –msR(t, a, 2, 2) * –msR(a, t, 3, 3) * –merge(a, t, 2, 3, 3) merge(a, t, 0, 2, 3) * will not be traced –mSR(a, t, 4, 8) mSR(t, a, 4, 5) –mSR(t, a, 4, 4) * –mSR(a, t, 5, 5) * –merge(a, t, 4, 5, 5) mSR(a, t, 6, 8) –mSR(t, a, 6, 6) * –mSR(a, t, 7, 8) mSR(t, a, 7, 7) * mSR(a, t, 8, 8) * merge(a, t, 7, 8, 8) –merge(a, t, 6, 7, 8) merge(a, t, 4, 6, 8) –merge(a, t, 0, 4, 8)

©Duane Szafron Merge Sort Trace mSR(0,8) mSR(0,3) mSR(0,1) mergeSort()

©Duane Szafron Merge Sort Trace merge(0,1,1) mSR(2,3) merge(2,3,3)

©Duane Szafron Merge Sort Trace 3 merge(0,2,3) mSR(4,8) mSR(4,5)

©Duane Szafron Merge Sort Trace 4 merge(4,5,5) mSR(6,8) mSR(7,8)

©Duane Szafron Merge Sort Trace 5 merge(7,8,8) merge(6,7,8) merge(4,6,8)

©Duane Szafron Merge Sort Trace 6 merge(0,4,8)

©Duane Szafron Counting Comparisons 1 How many comparison operations are required for a merge sort of an n-element collection? The recursive sort method calls merge() once, each time it divides the collection in half. Then there are n - 1 calls to merge: middle = (low + high) / 2; for (index = low; index < middle; index++) temp[index] = anArray[index]; mergeSortR(temp, anArray, low, middle - 1); mergeSortR(anArray, temp, middle, high); merge(anArray, temp, low, middle, high); i-1 where i = log n  1 log n 2 i-1 = n-1

©Duane Szafron Counting Comparisons 2 // as long as both collections are not empty while (bottIndex < middle) && (topIndex <= high) if (top[topIndex].compareTo(bottom[botIndex]) < 0) // copy from the top collection top[resultIndex++] = top[topIndex++]; else // or copy from the bottom collection top[resultIndex++] = bottom[botIndex++]; Assume that the collection is always divided exactly in half. Each time merge is executed for two lists of size k/2, it does comparisons in a loop and each time, either the bottom index or top index is incremented.

©Duane Szafron Comparisons - Best Case 1 In the best case, the same index (top or bottom) is incremented every time through the loop, so there are k/2 comparisons on each call to merge, for a call with two lists of size k/2. Notice that each level has the same total number of comparisons: n/2 k=4 k=2 k=1 k=2 k=1 k=4 k=2 k=1 k=2 k= best case comparisons per merge n=k=8

©Duane Szafron Comparisons - Best Case 2 Since the depth of the tree is log(n), the total number of comparisons is: n/2 * log(n) = O(n log(n))

©Duane Szafron Comparisons - Worst Case 1 In the worst case, the index that is incremented alternates each time through the loop, so there are k comparisons on each call to merge, for a call with two lists of size k/2. k=4 k=2 k=1 k=2 k=1 k=4 k=2 k=1 k=2 k= worst case comparisons per merge n=k=8

©Duane Szafron Comparisons - Worst Case 2 Since the depth of the tree is log(n), the total number of comparisons is: n * log(n) = O(n log(n))

©Duane Szafron Comparisons - Average Case The average case must be between the best case and the worst case so the average case is: O(n log(n))

©Duane Szafron Counting Assignments 1 // as long as both collections are not empty while (bottIndex < middle) && (topIndex <= high) if (top[topIndex].compareTo(bottom[botIndex]) < 0) // copy from the top collection top[resultIndex++] = top[topIndex++]; else // or copy from the bottom collection top[resultIndex++] = bottom[botIndex++]; How many assignment operations are required for a merge sort of an n-element collection? In the merge method, there is one assignment for every comparison:

©Duane Szafron Counting Assignments 2 However, the recursive sort method “copies” half of a collection of size k to the other collection which takes an extra k/2 assignments for each merge: middle = (low + high) / 2; for (index = low; index < middle; index++) temp[index] = anArray[index]; mergeSortR(temp, anArray, low, middle - 1); mergeSortR(anArray, temp, middle, high); merge(anArray, temp, low, middle, high);

©Duane Szafron Counting Assignments 3 In addition, during a merge, any remaining elements on the bottom (but not the top), must be copied: // copy any remaining elements from the bottom for (botIndex = botIndex; botIndex < middle; botIndex++) top[resultIndex++] = bottom[botIndex++];

©Duane Szafron Assignments - Best Case In the best case there are no remaining bottom elements so each merge has k/2 extra assignments. Since the depth of the tree is log(n), the total number of assignments is: n* log(n) = O(n log(n)) k=8 k=4 k=2 k=1 k=2 k=1 k=4 k=2 k=1 k=2 k= best case comparisons per merge best case extra assignments per merge

©Duane Szafron Assignments - Worst Case In the worst case there are k/2 remaining bottom elements so each merge has k extra assignments. Since the depth of the tree is log(n), the total number of assignments is: 2*n* log(n) = O(n log(n)) k=8 k=4 k=2 k=1 k=2 k=1 k=4 k=2 k=1 k=2 k= worst case comparisons per merge worst case Extra assignments per merge

©Duane Szafron Assignments - Average Case The average case is between the best and worst case so the average number of assignments must also be: O(n log(n)).

©Duane Szafron Time Complexity of Merge Sort Best case: O(n log(n)) for comparisons and assignments. Worst case O(n log(n)) for comparisons and assignments. Average case O(n log(n)) for comparisons and assignments. Note that the insertion sort is actually a better sort than the merge sort if the original collection is almost sorted.

©Duane Szafron Space Complexity of Merge Sort Besides the collection itself, an extra collection of references of the same size is needed. Therefore, the space complexity of Merge Sort is still O(n), but doubling the collection storage may sometimes be a problem. Does the recursion require extra space and if so, how much?