Download Homework Solutions: Algorithms and Complexity and more Assignments Computer science in PDF only on Docsity!
HOMEWORK 1 SOLUTIONS
Q1. Using the pseudocode from the lecture slide, to reverse the sort into nonincreasing instead of nondecreasing order just flip the A[i] > key to A[i] < key. Q2. #include void INSERTION_SORT(int* arr,const int n) { int i,j,k; for(i = 0; i<n; i++) // Element position for Comparsion { for(j = i; j>0; j--) // From Element position to the start of Array { if(arr[j-1] > arr[j]) // If Element is Smaller swap and shift j to the left { int temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; } else break; // if Element is Bigger Break Loop } } } Q3. bool isPalindrome(int x) { if(x< 0 || (x!= 0 &&x% 10 == 0 )) return false; int sum= 0 ; while(x>sum) { sum = sum* 10 +x% 10 ; x = x/ 10 ;
return (x==sum)||(x==sum/ 10 ); } Q4. void merge(vector& nums1, int m, vector& nums2, int n) { int i = m - 1 , j = n - 1 , tar = m + n - 1 ; while (j >= 0 ) { nums1[tar--] = i >= 0 && nums1[i] > nums2[j]? nums1[i--] : nums2[j--]; } } Q5. Big-O notation gives an upper bound of the time complexity function. In words, it generally means โThe complexity of the program is at most: this functionโ. If a time complexity function is said to be โat leastโ a certain function, it does not bound the time complexity function from above. With this example, the actual complexity could be much higher than n^2. Anything that grows faster than n^2, including n^3, n^30, and n^1000 are โat least O(n^2)โ. Saying this about a function gives no information about the longest possible time that a function will take to run, which is the goal of big-O. Q6. Yes, 2 n+1 = O(2 n ). 2 n+1 = 2 1 2 n = 22 n. By Big O notation, we may ignore the leading constant 2, giving us 2 n. We can also see that: 2 n+1 โค c2 n where c is a constant. Since 2 n+1 = 22 n , the constant c for the above equation is clearly 2, and 2 n+1 is O(2 n ). No, 2 2n โ O(2 n ). If we assume that 2 2n = O(2 n ), then there must exist some constant integer c such that 2 2n โค c2 n. 2 2n โค c2 n log(2 2n ) โค log(c2 n ) 2nlog(2) โค log(c) + nlog(2) 2nlog(2) - nlog(2) โค log(c) n*log(2) โค log(c) As n goes to infinity, it is clear that there is no constant integer c that can fulfill the above inequality. Therefore, 2 2n is not O(2 n ).
Q9.
Write a recurrence of the running time of recursive insertion sort. Using the following pseudocode: void recInsertion(arr, int) T(n) condition check 1 recInsertion(arr, n-1) n- while loop to sort n end function Here we end up with T(n) = T(n-1) + n which can be written more precisely as T(n) = T(n-1) + n for n> = 1 for n= Q. log 3 N < N < Nlog 3 N < Nlog 2 N < N^2 < N^3 < 2N Q11: Write and explain (with example) the worst case and best case complexity of insertion sort.
- Worst case: All elements in the unsorted array are in descending order, i.e. [5,4,3,2,1], the array has total n(n-1)/2 inversions, need O(n^2).
- Best case: All elements in the unsorted array are in ascending order, i.e. [1,2,3,4,5], the sorting process has total N-1 passes, need O(n). Q12: Let 3nlogn < n^2 โ 3logn < n โ when n = 10, 3logn = 9.96, n = 10, 3logn < n. The smallest number is 10.
Q13:
Step1: [19,31], [36,53], [11,51], [9,49] Step2: [19, 31, 36, 53], [9, 11, 49, 51] Step3: [9, 11, 19, 31, 36, 49, 51, 53] Q14: