Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.

Slides:



Advertisements
Similar presentations
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Advertisements

Introduction to Algorithms Quicksort
IS 2610: Data Structures Sorting Feb 16, Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the.
Winter 2006CISC121 - Prof. McLeod1 Stuff Assn 5 is available and due Friday, 7pm. Winding down! Possible Remaining topics: –Shell and Radix sorts (not.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CAN IT BE CONSIDERED AN EFFECTIVE SORT METHOD? Answer maybe for small data sets but definitely not for large sets. Nevertheless it is stable.
Radix Sorting CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Sorting Algorithms Bryce Boe 2012/08/13 CS32, Summer 2012 B.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Heapsort By: Steven Huang. What is a Heapsort? Heapsort is a comparison-based sorting algorithm to create a sorted array (or list) Part of the selection.
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.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
CS 171: Introduction to Computer Science II Quicksort.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Sorting CS221 – 3/2/09. Recursion Recap Use recursion to improve code clarity Make sure the performance trade-off is worth it Every recursive method must.
CHAPTER 11 Sorting.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
S: Application of quicksort on an array of ints: partitioning.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
CSE 373 Data Structures Lecture 15
1 MT258 Computer Programming and Problem Solving Unit 9.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Recursion, Complexity, and Sorting By Andrew Zeng.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Chapter 12 Recursion, Complexity, and Searching and Sorting
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
1 Radix Sort. 2 Classification of Sorting algorithms Sorting algorithms are often classified using different metrics:  Computational complexity: classification.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Static block can be used to check conditions before execution of main begin, Suppose we have developed an application which runs only on Windows operating.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Heap Sort Uses a heap, which is a tree-based data type Steps involved: Turn the array into a heap. Delete the root from the heap and insert into the array,
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Introduction A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order. Efficient sorting.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Radix Sorting CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Sorts, CompareTo Method and Strings
Count Sort, Bucket Sort, Radix Sort
Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Sorting by Tammy Bailey
Sorting (Heapsort, Mergesort)
ITEC 2620M Introduction to Data Structures
Divide and Conquer Merge sort and quick sort Binary search
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

Mergesort Overview Mergesort is a very popular sorting algorithm. The worst-case time complexity is Θ(Ν log N). – Remember that quicksort has a worst-case complexity of Θ(Ν 2 ). Thus, mergesort is preferable if we want a theoretical guarantee that the time will never exceed O(N log N). Also, unlike quicksort, mergesort is stable. A sorting method is called stable if it does not change the order of items whose keys are equal. Disadvantage: mergesort typically requires extra Θ(Ν) space, to copy the data. Quicksort on the other hand sorts in place: – No extra memory, except for a constant amount for local variables. – This can be important, if you are sorting massive amounts of data. 2

Mergesort Implementation Top-level function: Allocates memory for a scratch array. – Note that the size of the scratch array is the same as the size of the input array A. Then, we just call a recursive helper function that does all the work. 3 void mergesort(Item * A, int length) { Item * aux = malloc(sizeof(Item) * length); msort_help(A, length, aux); free(aux); } void msort_help(Item * A, int length, Item * aux) { if (length <= 1) return; int M = length/2; Item * C = &(A[M]); int P = length-M; msort_help(A, M, aux); msort_help(C, P, aux); merge(A, A, M, C, P, aux); }

Mergesort Implementation Recursive helper, basic approach: Split A to two halves. Left half: – Pointer to A. – Length M = length(A)/2. Right half: – Pointer C = &(A[M]). – Length P = length(A) - M. Sort each half. – Done via recursive call to itself. Merge the results (we will see how in a bit). 4 void mergesort(Item * A, int length) { Item * aux = malloc(sizeof(Item) * length); msort_help(A, length, aux); free(aux); } void msort_help(Item * A, int length, Item * aux) { if (length <= 1) return; int M = length/2; Item * C = &(A[M]); int P = length-M; msort_help(A, M, aux); msort_help(C, P, aux); merge(A, A, M, C, P, aux); }

Merging Two Sorted Sets Summary: – Assumption: arrays B and C are already sorted. – Merges B and C, produces sorted array D: Result array pointer D is passed as input. – It must already have enough memory. 5 void merge(Item *D, Item *B, int M, Item *C, int P, Item * aux ) { int T = M+P; int i, j, k; for (i = 0; i < M; i++) aux[i] = B[i]; for (j = 0; j < P; j++) aux[j+M] = C[j]; for (i = 0, j = M, k = 0; k < T; k++) { if (i == M) { D[k] = aux[j++]; continue; } if (j == T) { D[k] = aux[i++]; continue; } if (less(aux[i], aux[j])) D[k] = aux[i++]; else D[k] = aux[j++]; }

Merging Two Sorted Sets Note that here is where we use the aux array. Why do we need it? 6 void merge(Item *D, Item *B, int M, Item *C, int P, Item * aux ) { int T = M+P; int i, j, k; for (i = 0; i < M; i++) aux[i] = B[i]; for (j = 0; j < P; j++) aux[j+M] = C[j]; for (i = 0, j = M, k = 0; k < T; k++) { if (i == M) { D[k] = aux[j++]; continue; } if (j == T) { D[k] = aux[i++]; continue; } if (less(aux[i], aux[j])) D[k] = aux[i++]; else D[k] = aux[j++]; }

Merging Two Sorted Sets Note that here is where we use the aux array. Why do we need it? Using aux allows for D to share memory with B and C. We first copy the contents of both B and C to aux. Then, as we write into D, possibly overwriting B and C, aux still has the data we need. 7 void merge(Item *D, Item *B, int M, Item *C, int P, Item * aux ) { int T = M+P; int i, j, k; for (i = 0; i < M; i++) aux[i] = B[i]; for (j = 0; j < P; j++) aux[j+M] = C[j]; for (i = 0, j = M, k = 0; k < T; k++) { if (i == M) { D[k] = aux[j++]; continue; } if (j == T) { D[k] = aux[i++]; continue; } if (less(aux[i], aux[j])) D[k] = aux[i++]; else D[k] = aux[j++]; }

Merge Execution 8 Array D: position value Array aux: position value Array B: position01234 value Array C: Position01234 Value Initial state

Merge Execution 9 Array D: position value Array aux: position value Array B: position01234 value Array C: Position01234 Value Step 1: Copy B to aux.

Merge Execution 10 Array D: position k= value Array aux: positioni=01234j=56789 value Array B: position01234 value Array C: Position01234 Value Main loop initialization

Merge Execution 11 Array D: position0 k= value 10 Array aux: position0i=1234j=56789 value Array B: position01234 value Array C: Position01234 Value

Merge Execution 12 Array D: position01 k= value 1017 Array aux: position0i=12345j=6789 value Array B: position01234 value Array C: Position01234 Value

Merge Execution 13 Array D: position012 k= value Array aux: position0i=123456j=789 value Array B: position01234 value Array C: Position01234 Value

Merge Execution 14 Array D: position0123 k= value Array aux: position01i=23456j=789 value Array B: position01234 value Array C: Position01234 Value

Merge Execution 15 Array D: position01234 k= value Array aux: position01i=234567j=89 value Array B: position01234 value Array C: Position01234 Value

Merge Execution 16 Array D: position k=6 789 value Array aux: position01i= j=9 value Array B: position01234 value Array C: Position01234 Value

Merge Execution 17 Array D: position k=7 89 value Array aux: position01i= value Array B: position01234 value Array C: Position01234 Value j=10

Merge Execution 18 Array D: position k=8 9 value Array aux: position012i= value Array B: position01234 value Array C: Position01234 Value j=10

Merge Execution 19 Array D: position k=9 value Array aux: position0123i= value Array B: position01234 value Array C: Position01234 Value j=10

Merge Execution 20 Array D: position value Array aux: position01234i=56789 value Array B: position01234 value Array C: Position01234 Value j=10 k=10 k=10, so we are done!

Mergesort Execution Trace mergesort([35, 30, 17, 10, 60]) 21

Mergesort Execution Trace mergesort([35, 30, 17, 10, 60]) mergesort([35, 30]) 22

Mergesort Execution Trace mergesort([35, 30, 17, 10, 60]) mergesort([35, 30]) [30,35] mergesort([35]) [35] mergesort([30]) [30] merge([35],[30]) [30,35] 23

Mergesort Execution Trace mergesort([35, 30, 17, 10, 60]) mergesort([35, 30]) [30,35] mergesort([35]) [35] mergesort([30]) [30] merge([35],[30]) [30,35] mergesort([17, 10, 60]) mergesort([17]) [17] mergesort([10, 60]) 24

Mergesort Execution Trace mergesort([35, 30, 17, 10, 60]) mergesort([35, 30]) [30,35] mergesort([35]) [35] mergesort([30]) [30] merge([35],[30]) [30,35] mergesort([17, 10, 60]) mergesort([17]) [17] mergesort([10, 60]) [10, 60] mergesort([10]) [10] mergesort([60]) [60] merge([10],[60]) [10,60] 25

Mergesort Execution Trace mergesort([35, 30, 17, 10, 60]) mergesort([35, 30]) [30,35] mergesort([35]) [35] mergesort([30]) [30] merge([35],[30]) [30,35] mergesort([17, 10, 60]) [10,17,60] mergesort([17]) [17] mergesort([10, 60]) [10, 60] mergesort([10]) [10] mergesort([60]) [60] merge([10],[60]) [10,60] merge([17], [10,60]) [10,17,60] 26

Mergesort Execution Trace mergesort([35, 30, 17, 10, 60]) [10, 17, 30, 35, 60] mergesort([35, 30]) [30,35] mergesort([35]) [35] mergesort([30]) [30] merge([35],[30]) [30,35] mergesort([17, 10, 60]) [10,17,60] mergesort([17]) [17] mergesort([10, 60]) mergesort([10]) [10] mergesort([60]) [60] merge([10],[60]) [10,60] merge([17], [10,60]) [10,17,60] merge([30, 35], [10,17,60]) [10, 17, 30, 35, 60] 27

Mergesort Complexity Let T(N) be the worst-case running time complexity of mergesort for sorting N items. What is the recurrence for T(N)? 28

Mergesort Complexity Let T(N) be the worst-case running time complexity of mergesort for sorting N items. T(N) = 2T(N/2) + N. Let N = 2 n. T(N) = 2T(2 n-1 ) + 2 n = 2 2 T(2 n-2 ) + 2*2 n = 2 3 T(2 n-3 ) + 3*2 n = 2 4 T(2 n-4 ) + 4*2 n... = 2 n T(2 n-n ) + n*2 n = N * constant + N * lg N = Θ(N lg N). 29

Mergesort Variations There are several variations of mergesort that the textbook provides, that may be useful in different cases: Mergesort with no need for linear extra space. – More complicated, somewhat slower. Bottom-up mergesort, without recursive calls. Mergesort using lists and not arrays. You should be aware of them, but it is not really worth going over them in class, they reuse the basic concepts that we have already talked about. 30