Divide and Conquer (Merge Sort)

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

Back to Sorting – More efficient sorting algorithms.
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
2. Getting started Hsu, Lih-Hsing. Computer Theory Lab. Chapter 2P Insertion sort Example: Sorting problem Input: A sequence of n numbers Output:
Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
COMP 171 Data Structures and Algorithms Tutorial 3 Merge Sort & Quick Sort.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Divide-and-Conquer. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
1 Overview Divide and Conquer Merge Sort Quick Sort.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Algorithms Sorting – Part 3.
Advanced Sorting.
Lecture 2 Algorithm Analysis
Analysis of Algorithms CS 477/677
Fundamentals of Algorithms MCS - 2 Lecture # 11
Algorithm Design & Analysis
Introduction to Algorithms (2nd edition)
Divide and Conquer.
Growth Functions Algorithms Lecture 8
CS 583 Analysis of Algorithms
Divide and Conquer Approach
Proving correctness.
Ch 7: Quicksort Ming-Te Chi
Algorithms and Data Structures Lecture III
Ch 2: Getting Started Ming-Te Chi
Medians and Order Statistics
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS200: Algorithms Analysis
Divide and Conquer (Merge Sort)
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Divide & Conquer Algorithms
Merge Sort Overview.
Divide and Conquer Approach
nalysis of lgorithms A A Universidad Nacional de Colombia
Quicksort Quick sort Correctness of partition - loop invariant
Algorithms and Data Structures Lecture II
Presentation transcript:

Divide and Conquer (Merge Sort) Comp 122, Spring 2004

Divide and Conquer Recursive in structure Divide the problem into sub-problems that are similar to the original but smaller in size Conquer the sub-problems by solving them recursively. If they are small enough, just solve them in a straightforward manner. Combine the solutions to create a solution to the original problem Comp 122

An Example: Merge Sort Sorting Problem: Sort a sequence of n elements into non-decreasing order. Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted answer. Comp 122

Merge Sort – Example Original Sequence Sorted Sequence 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43 18 26 32 6 18 26 32 6 43 15 9 1 43 15 9 1 6 6 18 18 26 26 32 32 1 1 9 9 15 15 43 43 18 26 18 26 32 6 32 6 43 15 43 15 9 1 9 1 18 18 26 26 6 6 32 32 15 15 43 43 1 1 9 9 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 18 26 26 32 32 6 6 43 43 15 15 9 9 1 1 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 Comp 122

Merge-Sort (A, p, r) INPUT: a sequence of n numbers stored in array A OUTPUT: an ordered sequence of n numbers MergeSort (A, p, r) // sort A[p..r] by divide & conquer if p < r then q  (p+r)/2 MergeSort (A, p, q) MergeSort (A, q+1, r) Merge (A, p, q, r) // merges A[p..q] with A[q+1..r] Initial Call: MergeSort(A, 1, n) Comp 122

Procedure Merge Merge(A, p, q, r) 1 n1  q – p + 1 2 n2  r – q for i  1 to n1 do L[i]  A[p + i – 1] for j  1 to n2 do R[j]  A[q + j] L[n1+1]   R[n2+1]   i  1 j  1 for k p to r do if L[i]  R[j] then A[k]  L[i] i  i + 1 else A[k]  R[j] j  j + 1 Input: Array containing sorted subarrays A[p..q] and A[q+1..r]. Output: Merged sorted subarray in A[p..r]. Sentinels, to avoid having to check if either subarray is fully copied at each step. Comp 122

Merge – Example A L R … 6 1 8 6 8 26 9 32 26 1 32 9 42 42 43 43 … k k  R 1 1 9 9 42 42 43 43  i i i i i j j j j j Comp 122

Correctness of Merge Merge(A, p, q, r) Loop Invariant for the for loop 1 n1  q – p + 1 2 n2  r – q for i  1 to n1 do L[i]  A[p + i – 1] for j  1 to n2 do R[j]  A[q + j] L[n1+1]   R[n2+1]   i  1 j  1 for k p to r do if L[i]  R[j] then A[k]  L[i] i  i + 1 else A[k]  R[j] j  j + 1 Loop Invariant for the for loop At the start of each iteration of the for loop: Subarray A[p..k – 1] contains the k – p smallest elements of L and R in sorted order. L[i] and R[j] are the smallest elements of L and R that have not been copied back into A. Initialization: Before the first iteration: A[p..k – 1] is empty. i = j = 1. L[1] and R[1] are the smallest elements of L and R not copied to A. Comp 122

Correctness of Merge Merge(A, p, q, r) Maintenance: 1 n1  q – p + 1 2 n2  r – q for i  1 to n1 do L[i]  A[p + i – 1] for j  1 to n2 do R[j]  A[q + j] L[n1+1]   R[n2+1]   i  1 j  1 for k p to r do if L[i]  R[j] then A[k]  L[i] i  i + 1 else A[k]  R[j] j  j + 1 Maintenance: Case 1: L[i]  R[j] By LI, A contains p – k smallest elements of L and R in sorted order. By LI, L[i] and R[j] are the smallest elements of L and R not yet copied into A. Line 13 results in A containing p – k + 1 smallest elements (again in sorted order). Incrementing i and k reestablishes the LI for the next iteration. Similarly for L[i] > R[j]. Termination: On termination, k = r + 1. By LI, A contains r – p + 1 smallest elements of L and R in sorted order. L and R together contain r – p + 3 elements. All but the two sentinels have been copied back into A. Comp 122