The Merge Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Chapter 13 Recursion, Complexity, and Searching and Sorting
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
HST 952 Computing for Biomedical Scientists Lecture 9.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
The Quick Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.
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.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Sorting21 Recursive sorting algorithms Oh no, not again!
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Merge sort, Insertion sort
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
The Insertion Sort Mr. Dave Clausen La Cañada High School.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
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.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and.
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.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
 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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
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
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
Searching and Sorting Searching algorithms with simple arrays
Sorts, CompareTo Method and Strings
Fundamentals of Algorithms MCS - 2 Lecture # 11
The Bubble Sort Mr. Dave Clausen La Cañada High School
Sorting Mr. Jacobs.
Sorting Why? Displaying in order Faster Searching Categories Internal
David Kauchak cs201 Spring 2014
Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Unit-2 Divide and Conquer
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Selection Insertion and Merge
Ken Lambert & Doug Nance
Yan Shi CS/SE 2630 Lecture Notes
Basics of Recursion Programming with Recursion
Revised based on textbook author’s notes.
CSE 373 Data Structures and Algorithms
Data Structures Advanced Sorts Part 1: Mergesort
CPS120: Introduction to Computer Science
Merge Sort (11.1) CSE 2011 Winter April 2019.
CPS120: Introduction to Computer Science
The Binary Search by Mr. Dave Clausen
Mr. Dave Clausen La Cañada High School
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

The Merge Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen

Mr. Dave Clausen2 Merge Sort Description The essential idea behind merge sort is to make repeated use of a function that merges two lists, each already in ascending order, into a third list, also arranged in ascending order. The merge function itself only requires sequential access to the lists. Its logic is similar to the method you would use if you were merging two sorted piles of index cards into a third pile. –That is, start with the first card from each pile. – Compare them to see which one comes first, transfer that one over to the third pile, and advance to the next card in that pile. –Repeat that comparison, transfer, and advance operations until one of the piles runs out of cards. –At that point, move what is left of the remaining pile over the the third merged pile.

Mr. Dave Clausen3 Merge Sort Recursive Outline Merge sort employs a divide-and-conquer strategy to break the O(n 2 ) barrier. Here is an outline of the algorithm: –Compute the middle position of a vector and recursively sort its left and right subvectors (divide and conquer). –Merge the two sorted subvectors back into a single sorted vector. –Stop the process when subvectors can no longer be subdivided. –This top-level design strategy can be implemented as three C++ functions: – Merge_Sort: the function called by clients (int main or control menu execution). – MergeSortHelper: a helper function that hides the extra parameter required by recursive calls. –Merge: a helper function that implements the merging process.

Mr. Dave Clausen4 Merge Sort Parameters The merging process uses an extra vector, which we call copyBuffer (you can name this list_copy2). To avoid the overhead of allocating and deallocating the copyBuffer each time merge is called, the buffer is allocated once in mergeSort and subsequently passed to mergeSortHelper and merge. Each time mergeSortHelper is called, it needs to know the bounds of the sub vector with which it is working. These bounds are provided by two parameters, low and high.

Mr. Dave Clausen5 Here is the code for Merge_Sort: void Merge_Sort(apvector &v) { apvector copyBuffer(v.length()); MergeSortHelper(v,copyBuffer,0,v.length() - 1); } Remember to use logical_size instead of v.length(), and logical_size-1 instead of v.length()-1. You will also have to pass logical_size to Merge_Sort. After checking that it has been passed a subvector of at least two items, mergeSortHelper computes the midpoint of the subvector, recursively sorts the portions below and above the midpoint, and calls the merge function to merge the results. The Merge Sort Code C ++

Mr. Dave Clausen6 MergeSortHelper Code C++ void MergeSortHelper(apvector &v, apvector &copyBuffer, int low, int high) { // v is the vector being sorted // copyBuffer is temp space needed during merge // low, high are bounds of subvector // middle is the midpoint of subvector if (low < high) { int middle = (low + high) / 2; MergeSortHelper(v, copyBuffer, low, middle); MergeSortHelper(v, copyBuffer, middle + 1, high); Merge(v, copyBuffer, low, middle, high); } }

Mr. Dave Clausen7 Trace through Merge Sort The figure on the next slide shows the subvectors generated during recursive calls to mergeSortHelper, starting from a vector of eight items. Note that in this example the subvectors are evenly subdivided at each stage and there are 2 k-1 subvectors to be merged at stage k. Had the length of the initial vector not been a power of two, then an exactly even subdivision would not have been achieved at each stage and the last stage would not have contained a full complement of subvectors.

Mr. Dave Clausen8 Merge Sort Walk Through The figure on this slide shows the subvectors generated during recursive calls to mergeSortHelper, starting from a vector of eight items.

Mr. Dave Clausen9 Walk Through 2 Merging the sub vectors generated during a merge sort in order as they are merged.

Mr. Dave Clausen10 Merge Code C++ void Merge(apvector &v, apvector &copyBuffer, int low, int middle, int high) { int i1 = low, i2 = middle + 1, i; for (i = low; i <= high; i++) { if (i1 > middle) copyBuffer[i] = v[i2++]; //First subvector exhausted else if (i2 > high) copyBuffer[i] = v[i1++]; //Second subvector exhausted else if (v[i1] < v[i2]) copyBuffer[i] = v[i1++]; //Item in first subvector is less else copyBuffer[i] = v[i2++]; //Item in second subvector is less } for (i = low; i <= high; i++) //Copy sorted items back into v[i] = copyBuffer[i]; //proper position in a }

Mr. Dave Clausen11 Merge Description The merge function combines two sorted subvectors into a larger sorted subvector. The first subvector lies between low and middle and the second between middle + 1 and high. The process consists of three steps: –Set up index pointers to the first items in each subvector. These are at positions low and middle+1. –Starting with the first item in each subvector, repeatedly compare items. Copy the smaller item from its subvector to the copy buffer and advance to the next item in the subvector. Repeat until all items have been copied from both subvectors. If the end of one subvector is reached before the other’s, finish by copying the remaining items from the other subvector. –Copy the portion of copyBuffer between low and high back to the corresponding positions in the vector v.

Mr. Dave Clausen12 Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system. Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Merge Sort is O(nlog 2 n), because it takes approximately nlog 2 n passes to find the target element.