







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








































cs2420 | Introduction to Algorithms and Data Structures | Spring 2015
see recursion.
sum(N) = sum(N-1) + N public static int sum(int n) { if(n == 1) return 1; return sum(n-1) + n; } how can we solve the same problem without recursion? which is better, the recursive solution or the alternative? fix to handle zero or negative values...
divide and conquer
1)divide the array in half 2)sort the left half 3)sort the right half 4)merge the two halves together
void mergesort(int[] arr, int left, int right) { int mid = (left + right) / 2; mergsort(arr, left, mid); mergsort(arr, mid+1, right); merge(arr, left, mid+1, right); } divide conquer
void mergesort(int[] arr, int left, int right) { // arrays of size 1 are already sorted if(start >= end) return; int mid = (left + right) / 2; mergsort(arr, left, mid); mergsort(arr, mid+1, right); merge(arr, left, mid+1, right); } 19 divide conquer what is the complexity of the conquer step? A) c B) log N C) N D) N log N E) N 2 F) N 3
void mergesort(int[] arr, int left, int right) { // arrays of size 1 are already sorted if(start >= end) return; int mid = (left + right) / 2; mergsort(arr, left, mid); mergsort(arr, mid+1, right); merge(arr, left, mid+1, right); } 20 divide conquer what is the complexity of MERGESORT? A) c B) log N C) N D) N log N E) N 2 F) N 3 is this the worst || average || best-case?