Thus, the running time for this function can be expressed asįunction binarySearch is designed to find the (single) occurrence ofĪ special value is returned if \(K\) does not appear in the array. Is one more than the number of multiplications made by the recursiveīecause the base case does no multiplications, its cost is zero. In terms of the number of multiplication operations, Thus, the cost of the factorial function, if we wish to measure cost The result of this recursive call is then multiplied by the input Typically best expressed by a recurrence relation.įor example, the recursive factorial functionĬalls itself with a value one less than its input value. The running time for a recursive subroutine is Statement as being the cost of the more expensive branch.ĭetermining the execution time of a recursive We cannot simply count the cost of the if To perform an average-case analysis for such programs, Then clause only for the smallest of \(n\) values. If statement might be executed with probability \(1/n\).Īn example would be an if statement that executes the Various branches of an if or switch statement areįor example, for input of size \(n\), the then clause of an There are rare situations in which the probability for executing the Of the clauses (which is usually, but not necessarily, true).įor switch statements, the worst-case cost is that of the mostįor subroutine calls, simply add the cost of executing the subroutine. The size of \(n\) does not affect the probability of executing one This is also true for the average case, assuming that The cost of an if statement in the worst case is the greater of While loops are analyzed in a manner similar to for Where \(n\) is assumed to be a power of two and again Thus, the total cost of the loop is \(c_3\) times the sum of Until the last time through the loop when \(j = n\). You should see that for the first execution of the outer loop,įor the second execution of the outer loop, \(j\) is 2.Įach time through the outer loop, \(j\) becomes one greater, The cost of the inner loop is different because it costs The outer for loop is executed \(n\) times, but each time The expression sum++ requires constant time call itīecause the inner for loop is executed \(j\) times,īy simplifying rule (4) it has cost \(c_3j\). We work from the inside of the loop outward. The first for loop is a double loop and requires a special The second for loop is just like the one inĮxample 4.6.2 and takes \(c_2 n = \Theta(n)\) time. This code fragment has three separate statements: theįirst assignment statement and the two for loops.Īgain the assignment statement takes constant time Here is an illustration of the binary search method.Sum = 0 for ( j = 1 j <= n j ++ ) // First for loop for ( i = 1 i <= j i ++ ) // is a double loop sum ++ for ( k = 0 k < n k ++ ) // Second for loop A = k There are no positions remaining in the array that might contain the This process repeats until either the desired value is found, or Of the remaining positions from consideration. The value at this position again allows us to eliminate half Ignore all positions in the array less than \(mid\).Įither way, half of the positions are eliminated from furtherīinary search next looks at the middle position in that part of the Find the position in A that holds value K, if any does int sequential ( int A, int size, int K ) < K\), then you know that you can
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |