CMPT 125 Study Guide - Quiz Guide: Quicksort

162 views2 pages

Document Summary

// assumption: arr has at least n elements int fun(int arr[], int n) { if (n == 0) return 0; else return arr[n-1] + fun(arr, n-1); The function computes the sum of the first n elements of the array, i. e. , arr[0]+arr[1]+ + arr[n- For base case if n = 0, then the function returns 0. Induction step: for n>0, suppose that fun(arr,n-1) returns arr[0]+arr[1]+ + arr[n-2] -- the sum of the first n-1 elements. Then fun(arr,n) returns arr[n-1] + fun(arr,n-1) = arr[n-1] + (arr[0]+arr[1]+ + arr[n-2] ), which is exactly the sum of the first n element. Use the big-o notation to express the running time of fun() as a function of n. For n>1 we have t(n) = t(n-1) + 2 - we have the if-condition, the recursive call and addition. Solving this gives t(n) = 2n+1 = o(n) You don t need to explain how to implement the rearrange().

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers

Related Documents

Related Questions