Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms.

Similar presentations


Presentation on theme: "Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms."— Presentation transcript:

1 Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms.

2 Analysis of Selection Sort Algorithm 1 public static void sort(int[] a) { 2 for (int n = 0; n < a.length - 1; n++) { 3 int minPos = minimumPosition(a, n); 4 if (minPos != n) 5 ArrayUtil.swap(a, minPos, n); 6 } // End of for loop 7 } 1 public static int minimumPosition(int[] a, int from) { 2 int minPos = from; 3 for (int i = from + 1; i < a.length; i++) 4 if (a[i] < a[minPos]) 5 minPos = i; 6 return minPos; 7 } n(n+1) = O(n 2 )

3 Analysis of Insertion Sort algorithm public static void sort(int[] a) { int current, n, j; for (n = 1; n < a.length; n++) { current = a[n]; for (j = n; j > 0 && current < a[j-1]; j--) a[j] = a[j-1]; a[j] = current; } // End of outer loop. } // End of insertion sort method. O(n2)

4 Analysis of Merging Sort algorithm 1 mergeSort(int[] arr) { 2 while(the array arr has length greater than 1) { 3 Partition the array into two subarrays: 4 lowArray, highArray; 5 mergeSort(lowArray); 6 mergeSort(highArray); 7 merge(lowArray, highArray); 8 } // End of while loop 9 } // End of PseudoCode 21719513114815127 11135191215487217 192511134871215 19151757241213811 58241719111371215 An Ex. of the merge sort notice that it’s different than the one in the CD…

5 Analysis of Recursive Algorithms Analysis of recursive functions normally involves the formation and solving of recurrence relations. There are different methods of finding solutions to recurrence relations. We will solve recurrence relations by trying to find a closed form representation of a given recurrence relation. Finding closed forms for recurrence relations can be tedious or sometimes difficult.

6 Analysis of Recursive Binary Search Let's consider the code of recursive binary search algorithm shown below: 1 int recBinary(int[] a, int target, int lo, int hi) { 2 int mid = -1; 3 if(lo <= hi) { 4 mid = (hi + lo) / 2; 5 if(target < a[mid]) 6 mid = recBinary(a, target, lo, mid - 1); 7 else if(target > a[mid]) 8 mid = recBinary(a, target, mid + 1, hi); 9 } 10 return mid; 11 }

7 Analysis of Recursive Binary Search (Contd.) For arrays with a single element, the successful search will require n0 Op and has a constant complexity of O(1). We will consider this situation as the base case with n = 1. For an array with n elements, the situation will be as follows: For n > 1, each recursive call will compute the midpoint of the array and perform comparisons to determine which half of the array to search next. Let's assume that these comparisons will require n1 Op, for some constant n1.

8 Analysis of Recursive Binary Search (Contd.) f(n) = n 1 + f(n/2) f(n) = n 1 + n 1 + f(n/4) f(n) = 2n 1 + f(n/2 2 ) f(n) = n 1 +n 1 + n 1 + f(n/8) f(n) = 3n 1 + f(n/2 3 ). f(n) =kn 1 + f(n/2 k ) f(n) = n 1 log 2 n + f(n/2 log 2 n ) f(n) = n 1 log 2 n + f(1) f(n) = n 1 log 2 n + n 0

9 Analysis of Recursive Selection Sort Let's consider the code of recursive selection sort algorithm shown below: 1 int findMin(int[] a, int n) { 2 int j = n; 3 for(int i = 0; i < n; ++i) 4 if(a[i] < a[j]) 5 j=i; 6 return j; 7 } 8 void selectionSort(int[] a, int n) { 9 int minPos, temp; 10 if(n > 0) { 11 minPos = findMin(a, n); 12 temp = a[n]; 13 a[n] = a[minPos]; 14 a[minPos] = temp; 15 selectionSort(a, n-1) 16 } 17 }

10 Analysis of Recursive Selection Sort (Contd.) Assume that a, b 1 and b 2 are some integer constants. With the above assumptions, the number of operations performed by findMin() and swap() methods is: Cost of findMin() = an + b 1 ; Cost of swap() = b 2 Let's assume also that the cost of selectionSort(a, n) call is: f(n) and that of selectionSort(a, n-1) is therefore f(n-1). Then, for the base case we have: f(0) = c, for some constant c. For any value of n > 0, we will then have: f(n) = a n + b + f(n-1), n > 0 and b = b 1 + b 2

11 Analysis of Recursive Selection Sort (Contd.) f(n) = an + b + f(n-1) = an + b + a(n-1) +b +f(n-2) = an + b + a(n-1) +b + a(n-2) + b +f(n-3). = an + b + a(n-1) +b + a(n-2) + b +... + a(1) + b +f(0) = an + b + a(n-1) +b + a(n-2) + b +... + a(1) + b + c = [a + a(n-1) +... + a(1)] + nb +c = a[ n(n+1) / 2 ] +nb +c = O(n 2 )

12 Analysis of Tower of Hanoi Problem As a a final case study, we will consider the tower of Hanoi problem. The tower of Hanoi problem can be stated as follows: –There are three vertical poles mounted on a platform. –n disks of different sizes are provided initially stacked on pole 1. –Only a single disk can be moved at a time from the top of any pole to the top of another pole. –No larger disk can be moved on top of a smaller disk. To summarize, the tower of Hanoi problem is to move the n disks from pole 1 to pole 2 following the above rules. While pole 3 can be used as a temporary disk holder. »Don’t Forget to see the Tower of Hanoi animation on the CD.

13 Analysis of Tower of Hanoi Problem (Contd.) 1 public class TowerofHanoi { 2 void moveDisk(int count, int start, int finish, int temp) { 3 if(count > 0) { 4 moveDisks(count-1, start, temp, finish); 5 System.out.println("Moving a disk from " + start + " to " + finish); 6 moveDisks(count-1, temp, finish, start); 7 } 8 } // End of MoveDisks() method 9 10 public static void main(String[] args) { 11 static final int DISKS = 6; 12 moveDisks(DISKS, 1, 3, 2); 13 } // End of main() method 14} // End of TowerofHanoi class

14 Analysis of Tower of Hanoi Problem (Contd.) As you have noticed from the previous demo, the cost of moving the disks is as follows moves(n) = 1if n = 1 moves(n) = 1 + 2 moves(n-1)if n = 1 moves(n) = 1 + 2 moves (n-1) = 1 +2[1 + 2 moves (n-2)] = 1 +2[1 + 2 2[1 + 2 moves (n-3)]] = …………………………………… = 1 + 2 + 2 2 + 2 3 +………….+ 2 n-1 = 2 0 + 2 1 + 2 2 + 2 3 +………….+ 2 n-1 = O(2 n- )

15 Drill Question 1.Find the complexity function of the merge() method shown below: static void merge(int[] a1, int l1, int r1,int[] a2, int l2, int r2,int[] a3, int l3){ int i = l1, j = l2, k = l3; while(i <= r1 && j <= r2) { if(a1[i] <= a2[j]) a3[k++] = a1[i++]; else a3[k++] = a2[j++]; } while(i <= r1) a3[k++] = a1[i++]; while(j <= r2) a3[k++] = a2[j++]; } // End of merge() method 2.Derive the complexity analysis of the merge sort algorithm.


Download ppt "Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms."

Similar presentations


Ads by Google