A BRIEF LOOK AT ONE OF THE EARLIER SORTING ALGORITHMS Merge Sort.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Garfield AP Computer Science
Lecture 3: Parallel Algorithm Design
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Dynamic Programming.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
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.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
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.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L18 (Chapter 23) Algorithm.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
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.
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.
Recursion, Complexity, and Sorting By Andrew Zeng.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
File Organization and Processing Week 13 Divide and Conquer.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
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.
Order Statistics David Kauchak cs302 Spring 2012.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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.
 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.
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.
1 Chapter 8 Sorting. 2 OBJECTIVE Introduces: Sorting Concept Sorting Types Sorting Implementation Techniques.
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.
SORTING ALGORITHMS Christian Jonsson Jonathan Fagerström And implementation.
Merge Sort Presentation By: Justin Corpron. In the Beginning… John von Neumann ( ) Stored program Developed merge sort for EDVAC in 1945.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Chapter 7 Sorting Spring 14
ITEC 2620M Introduction to Data Structures
Divide and Conquer.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
MergeSort Source: Gibbs & Tamassia.
Chapter 4: 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.
Sorting Algorithms Ellysa N. Kosinaya.
ITEC 2620M Introduction to Data Structures
Presentation By: Justin Corpron
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Analysis of Algorithms
CENG 351 Data Management and File Structures
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.
CSCE 3110 Data Structures & Algorithm Analysis
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

A BRIEF LOOK AT ONE OF THE EARLIER SORTING ALGORITHMS Merge Sort

History of the Merge Sort Written around 1945 Commonly attributed to renown Hungarian- American mathematician John von Neumann One of the first sorting styles proposed for computers Uses divide and conquer principles (recursively breaking down a problem into smaller sub- problems)

What does a merge sort look like? Graphic courtesy of Wikimedia Commons

So what’s happening exactly? The data is recursively broken down into smallest possible subset (in this case, an individual number) The first item in one subset is compared to the first in the next subset The smaller of the two is placed first in a new set, then the second smallest etc. Increasingly larger sorted sets are formed until the final, fully sorted list is made by comparing one sorted half of the data to the other sorted half

Let’s try our own merge sort Here we have the months of the year, except somehow they were placed out of order March NovemberAprilOctoberJanuaryJune FebruaryDecemberSeptemberMayJulyAugust

First, divide the list into individual months

Now, compare the first two March comes before November, so March will be added to the new set first and then November second

Now compare the rest of the sets

Compare again The first item in the first two groups are compared and the earlier month is added to the new set

Round 2 …and then the second item in the first group is compared with the first item of the second group

The newly sorted groups

Compare the first two groups…

Building the new set

And sorted

One final merge sort

Building the final set

Sort complete!

Class Demonstrati0n Get eight volunteers to stand in a line at the front of the classroom Have each draw a number out of a hat Perform a merge sort on the volunteers by comparing their numbers and grouping accordingly Alternatively, use height for sorting if that can be done without hurting morale

Merge Sorts and Binary Trees The merge sort algorithm can be visualized using a binary tree, where:  Each leaf node is one piece of data  Going up one level involves one merge process Image courtesy of Rashid Bin Muhammad at Kent State University

Time Complexity Best case: O(n)  When values are already sorted Worst case: O(n log n )  n operations / level * log n + 1 levels  When the data is unsorted/randomly arranged Average case: O(n log n ) The only stable O(n log n ) sorting algorithm

Memory Requirements Array implementation  Generally requires a second array to store the merged list  O( n ) extra space Linked list implementation  Can be done in place by changing list pointers  O( log n ) extra space [for recursion] Recursive nature of algorithm requires extra memory compared to a non-recursive algorithm

When should you use merge sort? For linked lists When wanting to avoid random access to data If stability is desired When using extra space is not a concern

References ciencetheory/mergesort.html ciencetheory/mergesort.html hms/MyAlgorithms/Sorting/mergeSort.htm hms/MyAlgorithms/Sorting/mergeSort.htm