ALG0183 Algorithms & Data Structures Lecture 16 Merge sort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case,

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Garfield AP Computer Science
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
ALG0183 Algorithms & Data Structures Lecture 14 Selection Sort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case,
ALG0183 Algorithms & Data Structures Lecture 3 Algorithm Analysis 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5 Sahni.
1 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.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Using Divide and Conquer for Sorting
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
ALG0183 Algorithms & Data Structures Lecture 17 Quicksort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting21 Recursive sorting algorithms Oh no, not again!
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Merge sort, Insertion sort
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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.
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.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
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 19
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
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.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Analysis of Algorithms
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Data Structure Introduction.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
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.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
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)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Today’s Material Sorting: Definitions Basic Sorting Algorithms
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.
1 Algorithms Searching and Sorting Algorithm Efficiency.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 4 Divide-and-Conquer
Quicksort and Mergesort
Chapter 4: Divide and Conquer
Hassan Khosravi / Geoffrey Tien
CSE 373 Data Structures and Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

ALG0183 Algorithms & Data Structures Lecture 16 Merge sort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case, average case, and best-case O(nlogn) usually not in-place algorithm (additional list when merging) usually stable implementations The main disadvantage of merge sort is that it uses extra memory proportional to n. If you are sorting a very big list you will need a lot of memory. Merge sort is stable if the merge function is stable. recursion/sjálfkvaðning recursive/endurkvæmur Chapter 8.5

Merge Sort definition 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 Definition: A sort algorithm that splits the items to be sorted into two groups, recursively sorts each group, and merges them into a final, sorted sequence. Run time is Θ(n log n).sortrecursivelymergesΘ Θ Formal Definition: f(n) = Θ (g(n)) means there are positive constants c 1, c 2, and k, such that 0 ≤ c 1 g(n) ≤ f(n) ≤ c 2 g(n) for all n ≥ k. The values of c 1, c 2, and k must be fixed for the function f and must not depend on n.

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3 Recursion definition An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task. Note: Every recursive solution involves two major parts or cases, the second part having three components. base case(s), in which the problem is simple enough to be solved directly, and recursive case(s). A recursive case has three components: divide the problem into one or more simpler or smaller parts of the problem, call the function (recursively) on each part, and combine the solutions of the parts into a solution for the problem. Depending on the problem, any of these may be trivial or complex. Note: it can be possible to run into stack memory problems if the depth of the recursion is large. For merge sort, however, the depth of the recursion is O(log 2 n) – see later.

History & Ongoing Development “A Meticulous Analysis of Mergesort Programs”, Jyrki Katajainen and Jesper Larsson Träff, Lecture Notes In Computer Science, Vol. 1203, Proceedings of the Third Italian Conference on Algorithms and Complexity, pp 217 – 228, /25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 “Mergesort is as important in the history of sorting as sorting in the history of computing. A detailed description of bottom-up mergesort, together with a timing analysis, appeared in a report by Goldstine and von Neumann [6] as early as Today numerous variants of the basic method are known, for instance, top-down mergesort (see, e.g., [17, pp ]), queue mergesort [7], in-place mergesort (see, e.g., [8]), natural mergesort (see, e.g., [11, pp ]), as well as other adaptive versions of mergesort (see [5, 14] and the references in these surveys). The development in this paper is based on bottom-up mergesort, or straight mergesort as it was called by Knuth [11, pp ].” “New implementations for two-way and four-way bottom-up mergesort are given, the worst-case complexities of which are shown to be bounded by 5.5nlog 2 n + O(n) and 3.25nlog 2 n+O(n), respectively.”

Step-by-step example 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 Step-by-step example from a student paper on Merge sort by Luis Quiles

Pseudocode implementation 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 Pseudocode implementation

Lewis code © Addison Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9 public static void mergeSort (Comparable[] data, int min, int max) { if (min < max) { int mid = (min + max) / 2; mergeSort (data, min, mid); mergeSort (data, mid+1, max); merge (data, min, mid, max); }

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10 Lewis code © Addison Wesley public static void merge (Comparable[] data, int first, int mid, int last) { Comparable[] temp = new Comparable[data.length]; int first1 = first, last1 = mid; // endpoints of first subarray int first2 = mid+1, last2 = last; // endpoints of second subarray int index = first1; // next index open in temp array // Copy smaller item from each subarray into temp until one // of the subarrays is exhausted while (first1 <= last1 && first2 <= last2) { if (data[first1].compareTo(data[first2]) < 0) { temp[index] = data[first1]; first1++; } else { temp[index] = data[first2]; first2++; } index++; }

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 Lewis code © Addison Wesley // Copy remaining elements from first subarray, if any while (first1 <= last1) { temp[index] = data[first1]; first1++; index++; } // Copy remaining elements from second subarray, if any while (first2 <= last2) { temp[index] = data[first2]; first2++; index++; } // Copy merged data into original array for (index = first; index <= last; index++) data[index] = temp[index]; }

Stable or unstable? Record keys are a,b, and c. There are 4 records. Two records have the same key b. Let x and y subscripts be used to distinguish records with key b. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 bxbx byby ca bxbx byby ca bxbx cbyby a if (data[first1].compareTo(data[first2]) < 0) { temp[index] = data[first1]; first1++; } else { temp[index] = data[first2]; first2++; } Record b x is not less than b y, so b y is placed into the temp array first. (Applies to all test cases.) Code should be <=0, not <0. Lewis code is unstable. Check for yourself. idealised

Weiss code, Figure 8.8 © Addison Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 Weiss code, Figure 8.9 © Addison Wesley Weiss code is stable.

“Selecting the Right Algorithm” Talk given by Michail G. Lagoudakis at the 2001 AAAI Fall Symposium Series: Using Uncertainty within Computation, Cape Cod, MA, November /25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 “Selecting the Right Algorithm” Talk given by Michail G. Lagoudakis at the 2001 AAAI Fall Symposium Series: Using Uncertainty within Computation, Cape Cod, MA, November Earlier cross-over point.

The dashed line represents the algorithm which starts with Insertion Sort and then swaps over to Quicksort at the cross-over point. The hybrid algorithm makes use of knowledge of the interaction between algorithms and performs even better: Insertion Sort, then Merge Sort, then Quicksort. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17 “Selecting the Right Algorithm” Talk given by Michail G. Lagoudakis at the 2001 AAAI Fall Symposium Series: Using Uncertainty within Computation, Cape Cod, MA, November Optimal policies must be determined empirically i.e. do an experiment.

Many possible improvements can be made to merge sort. Sorting can be speeded up by choosing a more efficient algorithm for small n. – Advice is to use insertion sort for small n. The merge need not be performed if the highest element of the first subarray is less than the lowest element in the second subarray. (less than or equal to?) – Java´s merge sort has this improvement. – The improvement can certainly help for nearly ordered lists. – (There is a small cost finding the highest and lowest elements.) Four-way merging has been reported as being better than two-way merging. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18

Some Big-Oh merge sort (recursive) Assume the number of items N to be sorted is a power of 2. Assume the cost of merging two sublists of size N/2 is N comparisons. (actually N-1 comparisons) Let T(N) equal the number of comparisons needed to sort N items. – A proxy measure for the time needed. (ignoring moves/swaps) The time to sort N items is the time to sort two sublists of size N/2 plus the time to merge the two sublists together. The recurrence relation is: T(N) = 2 T(N/2) + N for N > 1 T(1) = 0 This recurrence relation is typical of many “divide-and- conquer” algorithms. There are several ways of proving that T(N) is Nlog 2 N 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 19

O(nlogn) proof by recursion tree 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks If the number of items N = 16, there are log 2 16 = 4 levels.