Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

ITEC200 Week10 Sorting. pdp 2 Learning Objectives – Week10 Sorting (Chapter10) By working through this chapter, students should: Learn.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
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
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.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Spring 2015 Lecture 5: QuickSort & Selection
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
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.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
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.
Sorting Chapter 10.
QuickSort QuickSort is often called Partition Sort. It is a recursive method, in which the unsorted array is first rearranged so that there is some record,
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
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.
CSE 373 Data Structures Lecture 19
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
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.
Chapter 12 Recursion, Complexity, and 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.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Advanced Sorting.
Advanced Sorting 7 2  9 4   2   4   7
Prof. U V THETE Dept. of Computer Science YMA
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sorting Chapter 8.
Lab 10 - Quicksort.
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
Chapter 4: Divide and Conquer
8/04/2009 Many thanks to David Sun for some of the included slides!
Sorting Chapter 8 CS 225.
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
CSE 373 Data Structures and Algorithms
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Sorting Chapter 10.
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Merge Sort

What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers. Output: permutation such that a' 1  a' 2  …  a' n. Example Start  End 

Why Sort?  A classic problem in computer science. Data requested in sorted order e.g., list students in increasing GPA order  Searching To find an element in an array of a million elements Linear search: average 500,000 comparisons Binary search: worst case 20 comparisons Database, Phone book  Eliminating duplicate copies in a collection of records  Finding a missing element, Max, Min 3

Sorting Algorithms Selection SortBubble Sort Insertion SortShell Sort T(n)=O(n 2 ) Quadratic growth In clock time Double input -> 4X time Feasible for small inputs, quickly unmanagable Halve input -> 1/4 time Hmm... can recursion save the day? If have two sorted halves, how to produce sorted full result? 4 10,0003 sec20,00017 sec 50,00077 sec100,0005 min

Divide and Conquer 1.Base case: the problem is small enough, solve directly 2.Divide the problem into two or more similar and smaller subproblems 3.Recursively solve the subproblems 4.Combine solutions to the subproblems 5

Merge Sort 6 Divide and conquer algorithm Worst case: O(nlogn) Stable maintain the relative order of records with equal values Input: 12, 5, 8, 13, 8, 27 Stable: 5, 8, 8, 12, 13, 27 Not Stable:5, 8, 8, 12, 13, 27

Merge Sort: Idea 7 Merge Recursively sort Divide into two halves FirstPart SecondPart FirstPart SecondPart A: A is sorted!

Merge-Sort: Merge 8 SortedSorted merge A: L: R: Sorted

Merge Example 9 L: R: A:

Merge Example cont L: A: R: i=4 j=4 k=8

Merge sort algorithm 11 MERGE-SORT A[1.. n] 1.If n = 1, done. 2.Recursively sort A[ 1..  n/2  ] and A[  n/2  +1.. n ]. 3.“ Merge ” the 2 sorted lists. Key subroutine: MERGE

Merge sort (Example) 12

Merge sort (Example) 13

Merge sort (Example) 14

Merge sort (Example) 15

Merge sort (Example) 16

Merge sort (Example) 17

Merge sort (Example) 18

Merge sort (Example) 19

Merge sort (Example) 20

Merge sort (Example) 21

Merge sort (Example) 22

Merge sort (Example) 23

Merge sort (Example) 24

Merge sort (Example) 25

Merge sort (Example) 26

Merge sort (Example) 27

Merge sort (Example) 28

Merge sort (Example) 29

Merge sort (Example) 30

Merge sort (Example) 31

Analysis of merge sort 32 MERGE-SORT A[1.. n] 1.If n = 1, done. 2.Recursively sort A[ 1..  n/2  ] and A[  n/2  +1.. n ]. 3.“ Merge ” the 2 sorted lists T(n)  (1) 2T(n/2)  (n)

Analyzing merge sort 33 T(n) =  (1) if n = 1; 2T(n/2) +  (n) if n > 1. T(n) =  (n lg n) (n > 1)

Recursion tree 34 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = log n cn #leaves = n (n)(n) Total  (n log n) …

Memory Requirement 35 Needs additional n locations because it is difficult to merge two sorted sets in place L: R: A:

Conclusion 36 Merge Sort: O(n log n) grows more slowly than  (n 2 ) asymptotically beats insertion sort in the worst case In practice, merge sort beats insertion sort for n > 30 or so Space requirement: O(n), not in-place

Heapsort

 Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional storage  As its name implies, heapsort uses a heap to store the array

First Version of a Heapsort Algorithm  When used as a priority queue, a heap maintains a smallest value at the top  The following algorithm  places an array's data into a heap,  then removes each heap item (O(n log n)) and moves it back into the array  This version of the algorithm requires n extra storage locations Heapsort Algorithm: First Version 1. Insert each value from the array to be sorted into a priority queue (heap). 2. Set i to 0 3. while the priority queue is not empty 4. Remove an item from the queue and insert it back into the array at position i 5. Increment i

Revising the Heapsort Algorithm  In heaps we've used so far, each parent node value was not greater than the values of its children  We can build a heap so that each parent node value is not less than its children  Then,  move the top item to the bottom of the heap  reheap, ignoring the item moved to the bottom

Trace of Heapsort

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Trace of Heapsort (cont.)

Revising the Heapsort Algorithm  If we implement the heap as an array  each element removed will be placed at the end of the array, and  the heap part of the array decreases by one element

Algorithm for In-Place Heapsort 1. Build a heap by rearranging the elements in an unsorted array 2. while the heap is not empty 3. Remove the first item from the heap by swapping it with the last item in the heap and restoring the heap property

Analysis of Heapsort  Because a heap is a complete binary tree, it has log n levels  Building a heap of size n requires finding the correct location for an item in a heap with log n levels  Each insert (or remove) is O(log n)  With n items, building a heap is O(n log n)  No extra storage is needed

Quicksort

 Developed in 1962  Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partioning)  all the elements in the left subarray are less than or equal to the pivot  all the elements in the right subarray are larger than the pivot  The pivot is placed between the two subarrays  The process is repeated until the array is sorted

Trace of Quicksort

Trace of Quicksort (cont.) Arbitrarily select the first element as the pivot

Trace of Quicksort (cont.) Swap the pivot with the element in the middle

Trace of Quicksort (cont.) Partition the elements so that all values less than or equal to the pivot are to the left, and all values greater than the pivot are to the right

Trace of Quicksort (cont.) Partition the elements so that all values less than or equal to the pivot are to the left, and all values greater than the pivot are to the right

Quicksort Example(cont.) is now in its correct position

Trace of Quicksort (cont.) Now apply quicksort recursively to the two subarrays

Trace of Quicksort (cont.) Pivot value = 12

Trace of Quicksort (cont.) Pivot value = 12

Trace of Quicksort (cont.) Pivot value = 33

Trace of Quicksort (cont.) Pivot value = 33

Trace of Quicksort (cont.) Pivot value = 33

Trace of Quicksort (cont.) Pivot value = 33 Left and right subarrays have single values; they are sorted

Trace of Quicksort (cont.) Pivot value = 33 Left and right subarrays have single values; they are sorted

Trace of Quicksort (cont.) Pivot value = 55

Trace of Quicksort (cont.) Pivot value =

Trace of Quicksort (cont.) Pivot value = 77

Trace of Quicksort (cont.) Pivot value = 77

Trace of Quicksort (cont.) Pivot value = 77

Trace of Quicksort (cont.) Left subarray has single value; it is sorted

Trace of Quicksort (cont.)

Algorithm for Quicksort  We describe how to do the partitioning later  The indexes first and last are the end points of the array being sorted  The index of the pivot after partitioning is pivIndex Algorithm for Quicksort 1. if first < last then 2. Partition the elements in the subarray first... last so that the pivot value is in its correct place (subscript pivIndex ) 3. Recursively apply quicksort to the subarray first... pivIndex Recursively apply quicksort to the subarray pivIndex last

Analysis of Quicksort  If the pivot value is a random value selected from the current subarray,  then statistically half of the items in the subarray will be less than the pivot and half will be greater  If both subarrays have the same number of elements (best case), there will be log n levels of recursion  At each recursion level, the partitioning process involves moving every element to its correct position—n moves  Quicksort is O(n log n), just like merge sort

Analysis of Quicksort (cont.)  The array split may not be the best case, i.e  An exact analysis is difficult (and beyond the scope of this class), but, the running time will be bounded by a constant x n log n

Analysis of Quicksort (cont.)  A quicksort will give very poor behavior if, each time the array is partitioned, a subarray is empty.  In that case, the sort will be O(n 2 )  Under these circumstances, the overhead of recursive calls and the extra run-time stack storage required by these calls makes this version of quicksort a poor performer relative to the quadratic sorts  We’ll discuss a solution later

Algorithm for Partitioning If the array is randomly ordered, it does not matter which element is the pivot. For simplicity we pick the element with subscript first

Trace of Partitioning (cont.) If the array is randomly ordered, it does not matter which element is the pivot. For simplicity we pick the element with subscript first

Trace of Partitioning (cont.) For visualization purposes, items less than or equal to the pivot will be colored blue; items greater than the pivot will be colored light purple

Trace of Partitioning (cont.) For visualization purposes, items less than or equal to the pivot will be colored blue; items greater than the pivot will be colored light purple

Trace of Partitioning (cont.) Search for the first value at the left end of the array that is greater than the pivot value

Trace of Partitioning (cont.) Search for the first value at the left end of the array that is greater than the pivot value up

Trace of Partitioning (cont.) Then search for the first value at the right end of the array that is less than or equal to the pivot value up

Trace of Partitioning (cont.) Then search for the first value at the right end of the array that is less than or equal to the pivot value updown

Trace of Partitioning (cont.) Exchange these values updown

Trace of Partitioning (cont.) Exchange these values

Trace of Partitioning (cont.) Repeat

Trace of Partitioning (cont.) Find first value at left end greater than pivot up

Trace of Partitioning (cont.) Find first value at right end less than or equal to pivot updown

Trace of Partitioning (cont.) Exchange

Trace of Partitioning (cont.) Repeat

Trace of Partitioning (cont.) Find first element at left end greater than pivot up

Trace of Partitioning (cont.) Find first element at right end less than or equal to pivot up down

Trace of Partitioning (cont.) Since down has "passed" up, do not exchange up down

Trace of Partitioning (cont.) Exchange the pivot value with the value at down up down

Trace of Partitioning (cont.) Exchange the pivot value with the value at down down

Trace of Partitioning (cont.) The pivot value is in the correct position; return the value of down and assign it to the pivot index pivIndex down

Algorithm for Partitioning

Code for partition when Pivot is the largest or smallest value

Revised Partition Algorithm  Quicksort is O(n 2 ) when each split yields one empty subarray, which is the case when the array is presorted  A better solution is to pick the pivot value in a way that is less likely to lead to a bad split  Use three references: first, middle, last  Select the median of the these items as the pivot

Trace of Revised Partitioning

Trace of Revised Partitioning (cont.) middle first last

Trace of Revised Partitioning (cont.) Sort these values middle first last

Trace of Revised Partitioning (cont.) Sort these values middle first last

Trace of Revised Partitioning (cont.) Exchange middle with first middle first last

Trace of Revised Partitioning (cont.) Exchange middle with first middle first last

Trace of Revised Partitioning (cont.) Run the partition algorithm using the first element as the pivot

Algorithm for Revised partition Method 1. Sort table[first], table[middle], and table[last] 2. Move the median value to table[first] (the pivot value) by exchanging table[first] and table[middle]. 3. Initialize up to first and down to last 4. do 5. Increment up until up selects the first element greater than the pivot value or up has reached last 6. Decrement down until down selects the first element less than or equal to the pivot value or down has reached first 7. if up < down then 8. Exchange table[up] and table[down] 9. while up is to the left of down 10. Exchange table[first] and table[down] 11. Return the value of down to pivIndex

Summary Comparison of Sort Algorithms

Sorting Algorithm Comparison 172 NameBestAverageWorstMemoryStable Bubble Sortnn2n2 n2n2 1yes Selection Sortn2n2 n2n2 n2n2 1no Insertion Sortnn2n2 n2n2 1yes Merge Sortnlogn nyes Quick Sortnlogn n2n2 log nno Heap Sortnlogn 1no