UNIT 5C Merge Sort Principles of Computing, Carnegie Mellon University

Similar documents
UNIT 5C Merge Sort. Course Announcements

We can use a max-heap to sort data.

Cpt S 122 Data Structures. Sorting

DATA STRUCTURE AND ALGORITHM USING PYTHON

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Unit-2 Divide and conquer 2016

Divide and Conquer. Algorithm D-and-C(n: input size)

CS 171: Introduction to Computer Science II. Quicksort

Sorting Pearson Education, Inc. All rights reserved.

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Lecture 19 Sorting Goodrich, Tamassia

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Introduction to Computers and Programming. Today

Divide-and-Conquer. Dr. Yingwu Zhu

Sorting is a problem for which we can prove a non-trivial lower bound.

algorithm evaluation Performance

Faster Sorting Methods

Algorithms. MIS, KUAS, 2007 Ding

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Mergesort again. 1. Split the list into two equal parts

Lecture 11: Recursion (2) - Binary search, Quicksort, Mergesort

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Quicksort (Weiss chapter 8.6)

15-110: Principles of Computing, Spring 2018

Sorting and Searching

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

DATA STRUCTURES AND ALGORITHMS

CSC 273 Data Structures

COMP Data Structures

Lecture Notes on Quicksort

Sorting. Task Description. Selection Sort. Should we worry about speed?

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Extended Introduction to Computer Science CS1001.py

CSC Design and Analysis of Algorithms

Lecture 8: Mergesort / Quicksort Steven Skiena

Lecture 6: Divide-and-Conquer

9. The Disorganized Handyman

BM267 - Introduction to Data Structures

Sorting Goodrich, Tamassia Sorting 1

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Lecture Notes on Quicksort

CompSci 105. Sorting Algorithms Part 2

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Divide and Conquer 4-0

Mergesort again. 1. Split the list into two equal parts

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

Divide and Conquer. Algorithm Fall Semester

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

CSCI 262 Data Structures. Recursive Function Analysis. Analyzing Power. Analyzing Power. Analyzing Power 3/31/2018

Merge Sort Goodrich, Tamassia Merge Sort 1

CS1 Lecture 30 Apr. 2, 2018

Teach A level Compu/ng: Algorithms and Data Structures

SORTING. Comparison of Quadratic Sorts

Recursion. Chapter 11. Chapter 11 1

About this exam review

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Data Structures and Algorithms

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Overview of Sorting Algorithms

QuickSort

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

Sorting Algorithms Day 2 4/5/17

Lecture 2: Getting Started

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

Searching in General

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Recursion. Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Sorting Algorithms. + Analysis of the Sorting Algorithms

Quick-Sort. Quick-Sort 1

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Real-world sorting (not on exam)

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

Intro to Algorithms. Professor Kevin Gold

STA141C: Big Data & High Performance Statistical Computing

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Advanced Algorithms and Data Structures

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Introduction to Computer Science and Programming for Astronomers

Transcription:

UNIT 5C Merge Sort 15110 Principles of Computing, Carnegie Mellon University 1

Divide and Conquer In computation: Divide the problem into simpler versions of itself. Conquer each problem using the same process (usually recursively). Combine the results of the simpler versions to form your final solution. Examples: Towers of Hanoi, Fractals, Binary Search, Merge Sort, Quicksort, and many, many more 4

Divide Group of 8 Groups of 4 Groups of 2 Groups of 1 Now each "group" is (trivially) sorted! 3

Conquer (merge sorted lists) 4

Conquer (merge sorted lists) 5

Conquer (merge sorted lists) 6

Merge Sort Input: List a of n elements. Output: Returns a new list containing the same elements in sorted order. Algorithm: 1. If less than two elements, return a copy of the list (base case!) 2. Sort the first half using merge sort. (recursive!) 3. Sort the second half using merge sort. (recursive!) 4. Merge the two sorted halves to obtain the final sorted array. 5

Merge Sort in Python def msort(list): if len(list) == 0 or len(list) == 1: # base case return list[:len(list)] # copy the input # recursive case halfway = len(list) // 2 list1 = list[0:halfway] list2 = list[halfway:len(list)] newlist1 = msort(list1) # recursively sort left half newlist2 = msort(list2) # recursively sort right half newlist = merge(newlist1, newlist2) return newlist 17

Merge Outline Input: Output: Algorithm: Two lists a and b, already sorted A new list containing the elements of a and b merged together in sorted order. 1. Create an empty list c, set index_a and index_b to 0 2. While index_a < length of a and index_b < length of b a. Add the smaller of a[index_a] and b[index_b] to the end of c b. increment the index of the list with the smaller element 3. If any elements are left over in a or b, add them to the end of c, in order 4. Return c 11

Filling in the details of Merge "Add the smaller of a[index_a] and b[index_b] to the end of c, and increment the index of the list with the smaller element": a.if a[index_a] b[index_b], then do the following: i. append a[index_a] to the end of c ii. add 1 to index_a b.otherwise, do the following: i. append b[index_b] to the end of c ii. add 1 to index_b 11

Filling in the details of Merge "If any elements are left over in a or b, add them to the end of c, in order": a.if index_a < the length of list a, then: i. append all remaining elements of list a to the end of list c, in order b.otherwise: i. append all remaining elements of list b (if any) to the end of list c, in order 11

Merge in Python def merge(a, b): index_a = 0 index_b = 0 c = [] while index_a < len(a) and index_b < len(b): if a[index_a] <= b[index_b]: c.append(a[index_a]) index_a = index_a + 1 else: c.append(b[index_b]) index_b = index_b + 1 # when we exit the loop # we are at the end of at least one of the lists c.extend(a[index_a:]) c.extend(b[index_b:]) return c 14

Example 1: Merge list a list b list c 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 12 44 58 62 29 31 74 80 12 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 12 44 58 62 29 31 74 80 12 29 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 12 44 58 62 29 31 74 80 12 29 31 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 12 44 58 62 29 31 74 80 12 29 31 44

Example 1: Merge (cont d) list a list b list c 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 12 44 58 62 29 31 74 80 12 29 31 44 58 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 12 44 58 62 29 31 74 80 12 29 31 44 58 62 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 12 44 58 62 29 31 74 80 12 29 31 44 58 62 74 80

Example 2: Merge list a list b list c 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 58 67 74 90 19 26 31 44 19 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 58 67 74 90 19 26 31 44 19 26 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 58 67 74 90 19 26 31 44 19 26 31 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 58 67 74 90 19 26 31 44 19 26 31 44 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7 58 67 74 90 19 26 31 44 19 26 31 44 58 67 74 90

Analyzing Efficiency Constant time operations: Comparing values and appending elements to the output. If you merge two lists of size i/2 into one new list of size i, what is the maximum number of appends that you must do? what is the maximum number of comparisons? Example: say we are merging two pairs of 2-element lists: with and with 8 appends for 8 elements If you have a group of lists to be merged pairwise, and the total number of elements in the whole group is n, the total number of appends will be n. Worse case number comparisons? n/2 or less, but still O(n) 18

How many merges? We saw that each group of merges of n elements takes O(n) operations. How many times do we have to merge n elements to go from n groups of size 1 to 1 group of size n? Example: Merge sort on 32 elements. Break down to groups of size 1 (base case). Merge 32 lists of size 1 into 16 lists of size 2. Merge 16 lists of size 2 into 8 lists of size 4. Merge 8 lists of size 4 into 4 lists of size 8. Merge 4 lists of size 8 into 2 lists of size 16. Merge 2 lists of size 16 into 1 list of size 32. In general: log 2 n merges of n elements. 5 = log 2 32 15110 Principles of Computing, Carnegie Mellon University 19

It takes log 2 n merges to go from n groups of size 1 to a single group of size n. Putting it all together Total number of elements per level is always n. It takes n appends to merge all pairs to the next higher level. Multiply the number of levels by the number of appends per level. 20

Big O In the worst case, merge sort requires O(n log2 n) time to sort an array with n elements. Number of operations Order of Complexity n log 2 n O(n log n) (n + n/2) log 2 n O(n log n) 4n log 10 n O(n log n) n log 2 n + 2n O(n log n) 21

O(N log N) Number of Operations (not drawn to scale) n log 2 n = O(n log n) 384 160 64 224 96 For an n log 2 n algorithm, the performance is better than a quadratic algorithm but just a little worse than a linear algorithm. 16 32 64 n (amount of data) 22

Merge vs. Insertion Sort n Insertion Sort (n(n+1)/2) Merge Sort (n log 2 n) Ratio 8 36 24 0.67 16 136 64 0.47 32 528 160 0.30 2 10 524,800 10,240 0.02 2 20 549,756,338,176 20,971,520 0.00004 23

Sorting and Searching Recall that if we wanted to use binary search, the list must be sorted. What if we sort the array first using merge sort? Merge sort O(n log n) (worst case) Binary search O(log n) (worst case) Total time(worst case): O(n log n) + O(log n) = O(n log n) 24

Comparing Big O Functions Number of Operations O(2 n ) O(n 2 ) O(n log n) O(n) O(log n) O(1) n (amount of data) 25

Merge Sort: Iteratively (optional) If you are interested, Explorations of Computing discusses an iterative version of merge sort which you can read on your own. This version uses an alternate version of the merge function that is not shown in the textbook but is given in PythonLabs. 26

Built-in Sort in Python Why we study sorting algorithms Practice in algorithmic thinking Practice in complexity analysis You will rarely need to implement your own sort function Python method list.sort takes a lists and modifies it while it sorts Python function sorted takes a list and returns a new sorted list Python uses timsort by Tim Peters (fancy!) 27

Quicksort Conceptually similar to merge sort Uses the technique of divide-and-conquer 1. Pick a pivot 2. Divide the array into two subarrays, those that are smaller and those that are greater 3. Put the pivot in the middle, between the two sorted arrays Worst case O(n 2 ) "Expected" O(n log n) 28

Next Time Data Organization 29