Intro to CS – Honors I Merge Sort GEORGIOS PORTOKALIDIS

Slides:



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

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Garfield AP Computer Science
CS 112 Introduction to Programming Sorting of an Array Debayan Gupta Computer Science Department Yale University 308A Watson, Phone:
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
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.
CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department.
Recursion Chapter 11 Chapter 11.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Chapter 7: Sorting Algorithms
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 1 An Introduction to Sorting.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Programming with Recursion. Example of Recursion (Ask Until the User Gets It Right) import java.util.* ; public class CountDown { private int count; public.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p

Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Sorting and Asymptotic Complexity
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
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.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
EFFICIENCY & SORTING II CITS Scope of this lecture Quicksort and mergesort Performance comparison.
Sort Algorithms.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Sorting: Advanced Techniques Smt Genap
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.
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
 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.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Basic Design Techniques iteration versus recursion divide-and-conquer an example: merge sort.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Merge Sort. In plain English: if the size of the array > 1, split the array into two halves, and recursively sort both halves; when the sorts return,
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting 9/13/2010. Introduction In CS1 you covered Insertion Sort, Bubble Sort, and Selection Sort. – In these algorithms we end up making a significant.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
CS 367 Introduction to Data Structures Lecture 11.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Using recursion for Searching and Sorting
Fundamentals of Algorithms MCS - 2 Lecture # 11
Sorting Mr. Jacobs.
CS 162 Intro to Programming II
Divide and Conquer.
Based on slides created by Ethan Apter & Marty Stepp
Basics of Recursion Programming with Recursion
CSE 373 Data Structures and Algorithms
slides adapted from Marty Stepp
Fundamental Structures of Computer Science II
Application: Efficiency of Algorithms II
Application: Efficiency of Algorithms II
Presentation transcript:

Intro to CS – Honors I Merge Sort GEORGIOS PORTOKALIDIS

MergeSort MergeSort can be very easily expressed using recursion ◦Also called Top-Down MergeSort A fine example of divide-and-conquer algorithm ◦Break down a problem to smaller pieces and attack those In simple words ◦The array to be sorted is divided in half ◦The two halves are sorted using recursion ◦The two sorted arrays are merged to form a single sorted array

MergeSort Algorithm 1. If the array a has only one element, do nothing (base case). Otherwise, do the following (recursive case): 2. Copy the first half of the elements in a to a smaller array named firstHalf. 3. Copy the rest of the elements in the array a to another smaller array named lastHalf. 4. Sort the array firstHalf using a recursive call. 5. Sort the array lastHalf using a recursive call. 6. Merge the elements in the arrays firstHalf and lastHalf into the array a

Visualizing MergeSort

Merge Process MergeSort divides an array into two parts ◦firstHalf and lastHalf ◦Both of these arrays are sorted  Their smallest element is in firstHalf[0] and lastHalf[0] The smallest element in both arrays is the smallest between firstHalf[0] and lastHalf[0] ◦We can copy/move that into the result array a Assuming the smallest element was in firstHalf[0], the next smallest element is the smallest between firstHalf[1] and lastHalf[0]

Merge Process int firstHalfIndex = 0, lastHalfIndex = 0, aIndex = 0; while (Some_Condition) { if (firstHalf[firstHalfIndex] < lastHalf[lastHalfIndex]) { a[aIndex] = firstHalf[firstHalfIndex]; aIndex++; firstHalfIndex++; } else { a[aIndex] = lastHalf[lastHalfIndex]; aIndex++; lastHalfIndex++; } What is the condition to terminate the loop? while ((firstHalfIndex < firstHalf.length) && (lastHalfIndex < lastHalf.length)) Loop until one of the arrays are exhausted The loop has moved all the elements of one array. So we just need to move the remaining elements into a

//Precondition: Arrays firstHalf and lastHalf are sorted from //smallest to largest; a. length = firstHalf.length + lastHalf.length. //Postcondition: Array a contains all the values from firstHalf //and lastHalf and is sorted from smallest to largest. private static void merge(int[] a, int[] firstHalf, int[] lastHalf) { int firstHalfIndex = 0, lastHalfIndex = 0, aIndex = 0; while ((firstHalfIndex < firstHalf.length) && (lastHalfIndex < lastHalf.length)) { if (firstHalf[firstHalfIndex] < lastHalf[lastHalfIndex]) { a[aIndex] = firstHalf[firstHalfIndex]; firstHalfIndex++; } else { a[aIndex] = lastHalf[firstHalfIndex]; lastHalfIndex++; } aIndex++; } //At least one of firstHalf and lastHalf has been completely //copied to a. Copy rest of firstHalf, if any. while (firstHalfIndex < firstHalf.length) { a[aIndex] = firstHalf[firstHalfIndex]; aIndex++; firstHalfIndex++; } //Copy rest of lastHalf, if any. while (lastHalfIndex < lastHalf.length) { a[aIndex] = lastHalf[lastHalfIndex]; aIndex++; lastHalfIndex++; }

Dividing an Array //Precondition: a.length = firstHalf.length + lastHalf.length. //Postcondition: All the elements of a are divided //between the arrays firstHalf and lastHalf. private static void divide(int[] a, int[] firstHalf, int[] lastHalf) { for (int i = 0); i < firstHalf.length; i++) firstHalf[i] = a[i]; for (int i = 0; i < lastHalf.length; i++) lastHalf[i] = a[firstHalf.length + i]; }

Back to MergeSort /** Precondition: Every indexed variable of the array a has a value. Postcondition: a[0] <= a[1] <=... <= a[a. length - 1]. */ public static void sort(int[] a) { if (a.length >= 2) { int halfLength = a.length / 2; int[] firstHalf = new int[halfLength]; int[] lastHalf = new int[a.length - halfLength]; divide(a, firstHalf, lastHalf); sort(firstHalf); sort(lastHalf); merge(a, firstHalf, lastHalf); } //else do nothing. a.length == 1, so a is sorted. } Why not create the arrays within divide?

MergeSort Characteristics For an array with n elements, we need to divide the array in half, similarly to a binary search If the length of the array is ◦odd, the array is split to segments of (n-1)/2 length ◦even, the array is split into a (n/2)-1 and a (n/2) segment So dividing the array requires x iterations, similarly to binary search, its worst case is x <= log 2 (n) However, for each divide we need to also merge the two halves, and in the worst case this requires n copies So in total we require k operations where k <= nlog2(n) The complexity in terms of performance is O(nlog(n)) MergeSort also has space overhead, it requires 2n locations

Other Versions of Merge Sort There are variants of MergeSort that do in-place sorting but it is slower ◦No additional space is required ◦Complexity rises to O(n log 2 n) ◦Additional reading ◦ ◦