CMPT 225 Lecture 10 – Merge Sort.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Sorting Algorithms Bryce Boe 2012/08/13 CS32, Summer 2012 B.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
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.
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.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Merge Sort.
Advanced Sorting.
Sort Algorithm.
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Fundamentals of Algorithms MCS - 2 Lecture # 11
CMPT 120 Topic: Searching – Part 2
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Lecture No.45 Data Structures Dr. Sohail Aslam.
Algorithm Efficiency and Sorting
CS 162 Intro to Programming II
Warmup What is an abstract class?
Section 10.3a Merge Sort.
David Kauchak cs201 Spring 2014
Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 7 Sorting Spring 14
Week 12 - Wednesday CS221.
Teach A level Computing: Algorithms and Data Structures
Insertion Sort
CSE 143 Lecture 23: quick sort.
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Advanced Sorting Methods: Shellsort
CSC212 Data Structure - Section RS
Quicksort analysis Bubble sort
CSC215 Lecture Algorithms.
Unit-2 Divide and Conquer
Hassan Khosravi / Geoffrey Tien
MSIS 655 Advanced Business Applications Programming
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Sorting Algorithms Ellysa N. Kosinaya.
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Presentation By: Justin Corpron
Yan Shi CS/SE 2630 Lecture Notes
Divide-and-Conquer The most-well known algorithm design strategy:
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Chapter 4.
Algorithms Dr. Youn-Hee Han April-May 2013
CSE 373 Data Structures and Algorithms
Ch. 2: Getting Started.
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide & Conquer Algorithms
Divide & Conquer Sorting
Algorithm Efficiency and Sorting
Alexandra Stefan Last updated: 4/16/2019
Algorithm Efficiency and Sorting
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Algorithm Efficiency and Sorting
Divide and Conquer Merge sort and quick sort Binary search
Advanced Sorting Methods: Shellsort
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CMPT 225 Lecture 10 – Merge Sort

Last Lecture We saw how to … Define the best, worst, and average case running time and space efficiency of insertion sort selection sort Considering the “simple” sorting algorithms, identify the most efficient one and explain why it is the most efficient

Learning Outcomes At the end of the next few lectures, a student will be able to: describe the behaviour of and implement simple sorting algorithms: insertion sort selection sort describe the behaviour of and implement more efficient sorting algorithms: quick sort merge sort analyze the best, worst, and average case running time (and space) of these sorting algorithms

Today’s menu Looking at Merge Sort Analyze its best, worst, and average case running time and space efficiency

How Merge Sort works Divide and conquer algorithm Merging method Recursive in nature If array has n elements Partitioning: we partition the array until we can’t partition anymore Merging: Then we sort the partitions as we are merging their elements until the whole array is sorted Sorting occurs while merging elements

How Merge Sort works – Partitioning step Problem statement -> sort this array:

How Merge Sort works – Merging step

Demo - Let’s have a look at Merge Sort

Merge Sort Algorithm -> mergeSort mergeSort(arr, low, high) if ( low < high ) mid = (low + high) / 2 mergeSort(arr, low, mid) // partitioning mergeSort(arr, mid + 1, high) // partitioning merge(arr, low, mid, mid + 1, high) // sorting

Merge Sort Algorithm -> merge merge(arr, low, mid, mid + 1, high) initialize indexes create temp array of size high - low + 1 while both subarrays contain unmerged elements if subarray1's current element is less or equal than subarray2's insert subarray1's current element in temp increment subarray1 and temp's indexes else insert subarray2's current element in temp increment subarray2 and temp's indexes while subarray1 contains unmerged items while subarray2 contains unmerged items copy temp back to the original (sub)array low ... high

Time Efficiency Analysis of Merge Sort - 1 Each time we “mergeSort” … We divide the data in half until the partitions have size 1 How many times does n have to be divided in half before the result is 1? Answer: log2 (n) times When we “merge” … We merge n elements at each level by comparing n – 1 elements (at most) at each level Merge sort performs n * log2 (n) operations at most For example: 8 4 4 2 2 2 2 1 1 1 1 1 1 1 1 3

When we “merge” … 1 3 4 5 2 6 7 9 Partition 1 Partition 2 Merged result 1 3 5 12 22 23 42 99 We have n elements (n = 8) Merge compares _______ times

Time Efficiency Analysis of Merge Sort - 2 Does the organization of the data in the array to be sorted affect the amount of work done by merge sort? Time efficiency of Best case: Average case: Worst case:

Space Efficiency Analysis of Merge Sort Not in-place algorithm How much space (memory) does merge sort require to execute? Therefore, its space efficiency is ->

Is merge sort stable?

√ Learning Check We can now … Describe merge sort Define the best, worst, and average case running time and space efficiency of merge sort

Next Lecture Another efficient sorting algorithm -> QuickSort