Growth Functions Algorithms Lecture 8
Many algorithms are recursive in structure to solve a given problem Recursive functions Many algorithms are recursive in structure to solve a given problem That means they call themselves recursively one or more times Algorithms lecture 8 Dr. Arwa Zabian
Divide and Conquer It breaks the problem into several sub problems that are similar to the original problem but smaller in size, solve the sub problems recursively, and then combine these solutions to create a solution to the original problem. Divide and conquer paradigm involves three steps at each level of recursion: Divide : the problem into a number of sub problems Conquer : solve the sub problems recursively Combine : the solution to the sub problems into the solution for the original problem Algorithms lecture 8 Dr. Arwa Zabian
Example: Merge Sort Algorithm Is an algorithm that uses divide and conquer approach to sort an array of size n It works as follow: Divide A into two sub arrays A1 , A2 of size n/2 ( this operation is done recursively until arriving at one element) Sort each sub array separately with Merge Sort Merge the two sorted sub arrays into the final array. Algorithms lecture 8 Dr. Arwa Zabian
Exercise Apply Merge sort on the following array The size of the array n=10 The solution : Divide a into two sub arrays of size 5 , repeat this operation until arriving at two sub arrays of size 2 each one. Sort these two sub arrays separately and then combine the two sorted arrays The solution is presented in the following figure:. Algorithms lecture 8 Dr. Arwa Zabian
Algorithms lecture 8 Dr. Arwa Zabian
Merge Sort Pseudo code MERGE-SORT (A, p,r) If p < r Then q (p + r)/2 Merge-Sort (A, p, q) Merge-Sort (A, q+1, r) Merge (A, p, q,r) Algorithms lecture 8 Dr. Arwa Zabian
Exercise Consider the following sequence of integers: Sort this sequence using Insertion sort and Merge sort Calculate the number of comparisons and the running time Algorithms lecture 8 Dr. Arwa Zabian
Analyze of Merge Sort When the sequence to be sorted has length 1, so it is sorted . The key operation in Merge sort is the combine or merge operations. For that, we use an auxiliary procedure MERGE (A, p, q, r) Where A is an array , and p, q, r are indices numbering elements of the array such that the subarrays A[p….q], and A[q+1,…..r] are sorted. It merges them to form a single sorted array. Merge sort take time (n), where n is the number of elements to be merged. If p r , the sub array has at most one element and is already sorted Algorithms lecture 8 Dr. Arwa Zabian
Analyzing Divide and Conquer Algorithms: The running time T(n) of divide and conquer is based on three steps: T(n) (1) if n c n too small a T(n/b) + D(n) + C(n) otherwise Number of Sub problems Time needed to solve each Sub problem Time of division Time of Merging So, we have a problem of size n We divide n into a sub problem, each one of size 1/b Algorithms lecture 8 Dr. Arwa Zabian
Analysis of Merge Sort Dividing steps D(n) = (1) Conquer solving the problem recursively 2T(n/2) of size n/2 Combine for n elements C(n)= (n) (1) if n=1 T(n) = 2 T(n/2)+ (n) if n > 1 T(n) = (n log n) where n is the size of the input Algorithms lecture 8 Dr. Arwa Zabian
Exercise.1 Suppose we are comparing implementation of Insertion sort and Merge sort on the same machine. For input of size n, insertion sort runs in 8n2 steps , while Merge sort runs in 64 n logn steps. For which values of n does insertion sort beat Merge sort? Algorithms lecture 8 Dr. Arwa Zabian
Exercise 2 Consider A is sorted array, how you can find the location and the value of the Mid point. And what characteristics does satisfy all the elements on the left and on the right of the Mid point. Write a pseudo code for finding the Mid point Algorithms lecture 8 Dr. Arwa Zabian
Exercise 3 What is the smallest value of n such that an algorithm whose running time is 100 n2 run faster than an algorithm whose running time is 2n on the same machine. Algorithms lecture 8 Dr. Arwa Zabian