Merge Sort. Merge Sort was developed by John von Neumann in 1945.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Sorting I / Slide 1 Mergesort Based on divide-and-conquer strategy * Divide the list into two smaller lists of about equal sizes * Sort each smaller list.
A BRIEF LOOK AT ONE OF THE EARLIER SORTING ALGORITHMS Merge Sort.
Copyright (C) Gal Kaminka Data Structures and Algorithms Sorting II: Divide and Conquer Sorting Gal A. Kaminka Computer Science Department.
Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
Merge sort csc326 information structures Spring 2009.
1 Merge Sort Merge Sort Reading p A Sorting Pattern The most efficient sorting algorithms all seem to follow a divide-and-conquer strategy.
Functional Design and Programming Lecture 4: Sorting.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Recursion, Complexity, and Sorting By Andrew Zeng.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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.
Merge Sort Presentation By: Justin Corpron. In the Beginning… John von Neumann ( ) Stored program Developed merge sort for EDVAC in 1945.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPTER 11.
Merge Sort Comparison Left Half Data Movement Right Half Sorted.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Sort Algorithm.
Sorts, CompareTo Method and Strings
Fundamentals of Algorithms MCS - 2 Lecture # 11
Sorting Mr. Jacobs.
Merging Merge. Keep track of smallest element in each sorted half.
CS 162 Intro to Programming II
Warmup What is an abstract class?
Python: Advanced Sorting Algorithms
Section 10.3a Merge Sort.
David Kauchak cs201 Spring 2014
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Chapter 7 Sorting Spring 14
Quick Sort.
Divide and Conquer.
10.6 Shell Sort: A Better Insertion
MergeSort Source: Gibbs & Tamassia.
Computer Architecture
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Advanced Sorting Methods: Shellsort
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
Unit-2 Divide and Conquer
Merge Sort Overview.
Mergesort Based on divide-and-conquer strategy
MSIS 655 Advanced Business Applications Programming
Sorting Algorithms Ellysa N. Kosinaya.
Topic 1: Problem Solving
Presentation By: Justin Corpron
Chapter 4.
Revised based on textbook author’s notes.
A G L O R H I M S T A Merging Merge.
Merge Sort (11.1) CSE 2011 Winter April 2019.
Topic: Divide and Conquer
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
A G L O R H I M S T A Merging Merge.
A G L O R H I M S T A Merging Merge.
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Applications of Arrays
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Merge Sort

Merge Sort was developed by John von Neumann in 1945.

John von Neumann Born: December 28, 1903 Died: February 8, 1957 Born in Budapest, Austria-Hungary A mathematician who made major contributions to set theory, functional analysis, quantum mechanics, ergodic theory, continuous geometry, economics and game theory, computer science, numerical analysis, hydrodynamics and statistics.

Merge Sort Merge Sort used a “divide-and-conquer” strategy. It’s a two-step process: – 1. Keep splitting the array in half until you end up with sub-arrays of one item (which are sorted by definition). – 2. Successively merge each sub-array together, and sort with each merge.

Merge Sort Let’s look at an example:

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

Merge Sort

PROGRAM MainMergeSort: Array = [44,23,42,33,16,54,34,18]; MergeSort(Array); PRINT Array; END. Merge Sort

PROGRAM MergeSort(Array): IF (length(Array) > 1) THEN MidPoint = len(Age)//2 LeftHalf = Age[:MidPoint] RightHalf = Age[MidPoint:] Merge Sort Keep recursively splitting the array until you get down sub-arrays of one element. Continued 

MergeSort(LeftHalf) MergeSort(RightHalf) LCounter = 0 RCounter = 0 MainCounter = 0 Merge Sort Recursively call MergeSort for each half of the array. After the splitting gets down to one element the recursive calls will pop off the stack to merge the sub-arrays together. Continued   Continued

WHILE (LCounter < len(LeftHalf) AND RCounter < len(RightHalf)) DO IF LeftHalf[LCounter] < RightHalf[RCounter] THEN Age[MainCounter] = LeftHalf[LCounter]; LCounter = LCounter + 1; ELSE Age[MainCounter] = RightHalf[RCounter]; RCounter = RCounter + 1; ENDIF; MainCounter = MainCounter + 1; ENDWHILE; Merge Sort Continued   Continued Keep comparing each element of the left and right sub-array, writing the smaller element into the main array

WHILE LCounter < len(LeftHalf) DO Age[MainCounter] = LeftHalf[LCounter]; LCounter = LCounter + 1; MainCounter = MainCounter + 1; ENDWHILE; WHILE Rcounter < len(RightHalf) DO Age[MainCounter] = RightHalf[RCounter] RCounter = RCounter + 1 MainCounter = MainCounter + 1 ENDWHILE; ENDIF; Merge Sort  Continued After the comparisons are done, write either the rest of the left array or the right array into the main array that

Merge Sort Complexity of Merge Sort – Best-case scenario complexity = O(N) – Average complexity = O(N * log 2 (N)) – Worst-case scenario complexity = O(N * log 2 (N))

etc.