Divide and Conquer Approach

Slides:



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

Lecture 3: Parallel Algorithm Design
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
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.
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.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
Insertion Sort Merge Sort QuickSort. Sorting Problem Definition an algorithm that puts elements of a list in a certain order Numerical order and Lexicographical.
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.
Merge sort csc326 information structures Spring 2009.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
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.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
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.
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],
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
Sorting: Advanced Techniques Smt Genap
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
 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.
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)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
Radix Sort We want to sort list L based on columns firstCol to lastCol. radixSort(L, firstCol, lastCol) { for(j = lastCol; j >= firstCol; j--) { for each.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
1 Overview Divide and Conquer Merge Sort Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Fundamentals of Algorithms MCS - 2 Lecture # 11
Lecture 3: Parallel Algorithm Design
Quick Sort and Merge Sort
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
Radix Sort We want to sort list L based on columns firstCol to lastCol. radixSort(L, firstCol, lastCol) { for(j = lastCol; j >= firstCol; j--) for each.
MergeSort Source: Gibbs & Tamassia.
CSC212 Data Structure - Section RS
Divide and Conquer Approach
Mergesort Based on divide-and-conquer strategy
Divide and Conquer (Merge Sort)
CSE 373 Data Structures and Algorithms
Sorting (Heapsort, Mergesort)
Topic 17 Faster Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Divide and Conquer (Merge Sort)
Copyright © Aiman Hanna All rights reserved
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
CSC 380: Design and Analysis of Algorithms
Divide & Conquer Algorithms
Merge Sort (11.1) CSE 2011 Winter April 2019.
Application: Efficiency of Algorithms II
Application: Efficiency of Algorithms II
CS 615: Design & Analysis of Algorithms
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
At the end of this session, Student will be able to:
Presentation transcript:

Divide and Conquer Approach

Divide and Conquer Approach Divide the problems into a number of sub problems. Conquer the sub problems by solving them recursively. If the sub-problem sizes are small enough, just solve the problems in a straight forward manner. Combine the solutions to the sub problems into the solution for the original problem.

Merge Sort Divide the n element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences to produce the sorted answer. Combine: Merge the two sorted sub sequences to produce the sorted answer.

Merge Sort Base Case: When the sequences to be sorted has length 1. 14 34 89 56 66 108 12 Sorted 108 56 12 14 89 34 66 Unsorted 108 56 14 66 Divide 56 66 108 14 Merge 34 12 89 Merge 12 89 34 Divide 108 66 Divide 14 56 Merge 56 14 Divide 12 89 Divide 89 12 Merge 34 Merge 34 Divide 34 BCase 66 108 Merge 89 Divide 89 BCase 89 Merge 12 BCase 12 Merge 12 Divide 66 BCase 66 Divide 66 Merge 108 Merge 108 Divide 108 BCase 56 Merge 56 Divide 56 BCase 14 BCase 14 Divide 14 Merge

Merge Sort Algorithm MergeSort(A, i, j) if j > i then mid ← (i + j)/2 MergeSort(A, i, mid ) MergeSort(A, mid + 1, j ) Merge(A, i, mid, j )

Merge Algorithm //LeftPos = start of left half; //RightPos = start of right half void Merge(int A[ ], int LeftPos, int RightPos, int RightEnd) { int LeftEnd = RightPos – 1; int TmpPos = 1 int NumElements = RightEnd – LeftPos + 1; int TempArray[NumElements]; while(leftPos <= LeftEnd && RightPos <= RightEnd) if(A[LeftPos] <= A[RightPos]) TmpArray[TmpPos++] = A[LeftPos++]; else TmpArray[TmpPos++] = A[RightPos++]; while(LeftPos <= LeftEnd) //Copy rest of first half while(RightPos <= RightEnd) //Copy rest of second half TmpArray[TmpPos++] = A[RightPos++]; for(int i = 1; i <= NumElements; i++) //Copy TmpArray back A[LeftPos++] = TmpArray[i]; }

Merge Algorithm The basic merging algorithms takes Two input arrays, A[] and B[], An output array C[] And three counters aptr, bptr and cptr. (initially set to the beginning of their respective arrays) The smaller of A[aptr] and B[bptr] is copied to the next entry in C i.e. C[cptr]. The appropriate counters are then advanced. When either of input list is exhausted, the remainder of the other list is copied to C.

Merge Algorithm 34 12 89 B[] bptr 56 66 108 14 A[] aptr 14 > 12 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 12 12 C[] cptr

Merge Algorithm 34 12 89 B[] 56 66 108 14 A[] aptr bptr 14 > 12 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 12 12 cptr++ cptr C[]

Merge Algorithm 34 12 89 B[] 56 66 108 14 A[] aptr bptr bptr++ 14 > 12 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 12 12 cptr++ C[] cptr

Merge Algorithm 56 66 108 14 A[] aptr 34 12 89 B[] bptr 14 < 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 14 14 C[] 12 cptr

Merge Algorithm 34 12 89 B[] 56 66 108 14 A[] aptr bptr 14 < 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 14 14 cptr++ C[] 12 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr aptr++ bptr 14 < 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 14 14 cptr++ C[] 12 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr 56 > 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 34 34 C[] 12 14 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr 56 > 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 34 14 cptr++ C[] 12 34 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr bptr++ aptr 56 > 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 34 14 cptr++ C[] 12 34 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr 56 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 56 56 C[] 12 14 34 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr 56 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 56 56 cptr++ cptr C[] 12 14 34

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr aptr++ bptr 56 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 56 56 cptr++ C[] 12 14 34 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr 66 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 66 66 C[] 12 14 34 56 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr 66 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 66 cptr++ C[] 12 14 34 56 66 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr aptr++ bptr 66 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 66 cptr++ C[] 12 14 34 56 66 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr 108 > 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 89 89 C[] 12 14 34 56 66 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr 108 > 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 89 89 cptr++ C[] 12 14 34 56 66 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr bptr++ 108 > 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 89 89 cptr++ C[] 12 14 34 56 66 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr Array B is now finished, copy remaining elements of array A in array C If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[] 12 14 34 56 66 89 cptr

Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr Array B is now finished, copy remaining elements of array A in array C If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[] 12 14 34 56 66 89 108 cptr

Merge Algorithm Analysis Suppose total number of elements to be merged is n Running time is O(n)

Merge Sort Analysis MergeSort computation tree has depth log n Associate some running time with each node in computation tree Sum up times at each node to find total running time

Computation Tree N = 7 lg 7  = 3 Tree Depth = 3 108 56 12 14 89 34 66 108 56 14 66 12 89 34 108 66 56 14 12 89 34 66 108 56 14 89 12

Computation of Time What is included in running time of a node: Time Spent at a node What is included in running time of a node: time to divide up input: O(1) time to make recursive calls: O(1) time to merge results of recursive calls: O(n) Total Time : O(n)

Merge Sort Analysis Consider one level at a time every element appears once on each level (except perhaps lowest level) n elements on each level O(n) running time per level O(log n) levels Total running time: O(n log n)

Problem with Merge Sort Merging two sorted lists requires linear extra memory. Additional work spent copying to the temporary array and back through out the algorithm. This problem slows down the sort considerably.