Hassan Khosravi / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

Garfield AP Computer Science
A simple example finding the maximum of a set S of n numbers.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
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.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
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.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Contest Algorithms January 2016 Pseudo-code for divide and conquer, and three examples (binary exponentiation, binary search, and mergesort). 5. Divide.
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)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
FASTER SORT using RECURSION : MERGE SORT COMP 103.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
CMPT 438 Algorithms.
Sorting.
Analysis of Algorithms CS 477/677
Fundamentals of Algorithms MCS - 2 Lecture # 11
Insertion sort Loop invariants Dynamic memory
Lecture 2 Sorting.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Top 50 Data Structures Interview Questions
Towers of Hanoi Move n (4) disks from pole A to pole C
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
COMP 53 – Week Seven Big O Sorting.
Quick Sort and Merge Sort
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Lecture 4 Divide-and-Conquer
Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Week 12 - Wednesday CS221.
Divide and Conquer.
Cinda Heeren / Geoffrey Tien
Quicksort and Mergesort
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Unit IV : Searching and sorting Techniques
CSC212 Data Structure - Section RS
SORTING AND SEARCHING.
Quicksort analysis Bubble sort
CSC215 Lecture Algorithms.
Unit-2 Divide and Conquer
Hassan Khosravi / Geoffrey Tien
C++ Plus Data Structures
Selection Insertion and Merge
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Chapter 4.
CSE 332: Data Abstractions Sorting I
Algorithms Dr. Youn-Hee Han April-May 2013
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
Data Structures Advanced Sorts Part 1: Mergesort
Searching/Sorting/Searching
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Divide and Conquer Merge sort and quick sort Binary search
Advanced Sorting Methods: Shellsort
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Hassan Khosravi / Geoffrey Tien Merge Sort Algorithm Analysis November 15, 2017 Hassan Khosravi / Geoffrey Tien

The story thus far... CPSC 259 topics up to this point Abstract data types Tools Pointers Priority queue Dynamic memory allocation Asymptotic analysis Stack Queue Dictionary Recursion Recurrence relations Data structures Algorithms Heapsort Linked list Circular array Array Sorting: Selection, Insertion Binary heap (Binary) (search) tree Divide and conquer paradigm (Merge sort and Quicksort November 15, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Merge sort Merge sort is an example of a divide-and-conquer algorithm that recursively splits the problem into smaller subproblems, solves them, and combines the subproblem solutions to form the overall solution Key steps in Merge sort: Split the array into halves Recursively sort each half Merge the two (sorted) halves together to produce a bigger, sorted array the actual sorting occurs in this merge step NOTE: The time to merge two sorted sub-arrays of sizes 𝑚 and 𝑛 is linear: 𝑂 𝑚+𝑛 November 15, 2017 Hassan Khosravi / Geoffrey Tien

Subarray merging of two sorted subarrays 1 4 8 3 5 7 1 3 4 5 7 8 November 15, 2017 Hassan Khosravi / Geoffrey Tien

Getting sorted subarrays Repeatedly divide arrays in half until each subarray contains a single element an element by itself is already sorted merging two single-element arrays is simply a single comparison The merge step copies the subarray halves into a temporary array and the merged elements are copied from the temporary array back to the original array November 15, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Merge sort example 8 1 4 5 3 7 8 1 4 5 3 7 8 1 4 5 3 7 8 1 5 3 1 8 3 5 1 4 8 3 5 7 1 3 4 5 7 8 November 15, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Algorithm void merge(int arr[], int low, int mid, int high) { int i = low, j = mid+1, index = 0; int* temp = (int*) malloc((high – low + 1) * sizeof(int)); while (i <= mid && j <= high) { if (arr[i] <= arr[j]) temp[index++] = arr[i++]; else temp[index++] = arr[j++]; } if (i > mid) { while (j <= end) else { while (i <= mid) for (index = 0; index < high-low; index++) arr[low + index] = temp[index]; free(temp); void msort(int arr[], int low, int high) { int mid; if (low < high) { // subarray has more than 1 element mid = (low + high) / 2; msort(arr, low, mid); msort(arr, mid+1, high); merge(arr, low, mid, high); } void mergeSort(int arr[], int size) { msort(arr, 0, size-1); } November 15, 2017 Hassan Khosravi / Geoffrey Tien

Merge sort example Again with proper recursion 8 1 4 5 3 7 8 1 4 5 3 7 November 15, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Merge sort analysis Recursion tree How many comparisons are made in the merge step? Worst case: 𝑛−1 comparisons need to check every subarray index Best case: 𝑛 2 comparisons reach the end of one subarray, copy the rest of the second subarray Still copying 𝑛 subarray items in any case How many times can the subarray be divided? log 2 𝑛 divisions to reach 1-element subarrays Overall: Θ 𝑛 log 𝑛 , all cases November 15, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Merge sort stability Stability can be enforced during the merge step at the following places: at the first while loop, prioritize duplicates from the left subarray (achieved by the <= in the condition) in the two conditional while loops (copying remaining subarray elements), copy in the same order encountered in the final for loop, keep all the elements in their existing order November 15, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Merge sort summary External sorting is a term for a class of sorting that can handle massive data sets that do not fit in RAM. Merge sort can be adapted to sort partial data sets brought into RAM from disk Merge sort is also highly parallelizable Name Best Average Worst Stable Memory Selection sort 𝑂 𝑛 2 challenging 𝑂 1 Insertion sort 𝑂 𝑛 Yes Merge sort 𝑂 𝑛 log 𝑛 November 15, 2017 Hassan Khosravi / Geoffrey Tien

Analysing recursive functions Recursive functions are defined in terms of themselves The running time on a given invocation can also be defined in terms of the running times of its own recursive invocations e.g 𝑇 𝑛 =something+𝑇 a smaller 𝑛 + … Like recursion, the running time on some very small input size can be determined immediately (a base case), typically 𝑇 1 Running time of subproblems can be similarly expressed in terms of running time of its subproblems Repeat the substitutions to establish a pattern Determine the number of substitution levels required to reach a base case Solve for a closed-form expression for running time November 15, 2017 Hassan Khosravi / Geoffrey Tien

Recurrence relations Analysis 𝑇 𝑛 ∈𝑂 𝑛 Example: Recursive max in an array double arrMax(double arr[], int size, int start) { if (start == size – 1) return arr[start]; else return max( arr[start], arrMax(arr, size, start + 1) ); } 𝑇 1 ≤𝑏 𝑇 𝑛 ≤𝑐+𝑇 𝑛−1 Analysis 𝑇 𝑛 ≤𝑐+𝑐+𝑇 𝑛−2 (by substitution) 𝑇 𝑛 ≤𝑐+𝑐+𝑐+𝑇 𝑛−3 (by substitution, again) 𝑇 𝑛 ≤𝑘∙𝑐+𝑇 𝑛−𝑘 (extrapolating, 0<𝑘≤𝑛) 𝑇 𝑛 ≤ 𝑛−1 ∙𝑐+𝑇 1 = 𝑛−1 ∙𝑐+𝑏 for 𝑘=𝑛−1 𝑇 𝑛 ∈𝑂 𝑛 November 15, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Merge sort analysis Now with even more math! Merge sort algorithm Split list in half, sort first half, sort second half, merge together 𝑇 1 ≤𝑏 𝑇 𝑛 ≤2∙𝑇 𝑛/2 +𝑐∙𝑛 Analysis ≤2 2∙𝑇 𝑛/4 +𝑐 𝑛/2 +𝑐𝑛 =4∙𝑇 𝑛/4 +𝑐∙𝑛+𝑐∙𝑛 ≤4 2∙𝑇 𝑛/8 +𝑐 𝑛/4 +𝑐∙𝑛+𝑐∙𝑛 =8∙𝑇 𝑛/8 +𝑐∙𝑛+𝑐∙𝑛+𝑐∙𝑛 ≤ 2 𝑘 ∙𝑇 𝑛/ 2 𝑘 +𝑘∙𝑐∙𝑛 extrapolating, 1<𝑘≤𝑛 ≤𝑛∙𝑇 1 +𝑐∙𝑛 log 𝑛 for 2 𝑘 =𝑛, or 𝑘=𝑛 log 2 𝑛 𝑇 𝑛 ∈𝑂 𝑛 log 𝑛 November 15, 2017 Hassan Khosravi / Geoffrey Tien

Readings for this lesson Thareja Chapter 14.10 (Merge sort) Next class Thareja Chapter 14.11 (Quicksort) November 15, 2017 Hassan Khosravi / Geoffrey Tien