This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Recursive Algorithms Margaret M. Fleck 19 March 2010 This lecture continues coverage of sorting and search algorithms (sections 3.1 and 3.3 of Rosen). 1 Binary search Last lecture, we saw a brief sketch of binary search. Lets look at this algo rithm in more detail. In many applications, its sensible to assume that the input array is sorted. That is a 1 a 2 . . . a n . Well see that it would take O ( n log n ) time to sort an unsorted array. But sometimes an array of data comes presorted. And sometimes an application will do many searches on an array of data thats constant or changes only very slowly, e.g. the registrars big database of student records. In such cases, the cost of sorting the array may be insignificant compared to the cost of doing the searches. When our input array is sorted, we can use a much more efficient algo rithms: binary search. Lets assume that our array is sorted with the smallest element at the start. (The same idea works, with small code changes, if the array is sorted in reverse order.) 1: binarysearch( a 1 ,. . . , a n : sorted array of reals, b real) 2: i = 1 3: j = n 4: while ( i < j ) 5: m = ( i + j ) / 2 1 6: if ( b > a m ) 7: then i = m+1 8: else j = m 9: if ( a i = b) return i 10: else return 0 The two variables i and j define a range of array positions. At the start of the function, this range is all of the input array. In each iteration of the loop, we shrink the range in half, either by making i larger or by making j smaller. The loop terminates when the range contains only a single array position i . To do the shrinking, we examine the value at the middle position in the current range. If its smaller than b , we know that b must live in the upper half of the range (if its in the array at all). Otherwise, b must live in the lower half of the range (again, if its in the array). 2 Analysis of binary search To do a bigO analysis of binary searchs running time, notice that the code in lines 13 and 910 takes constant time, i.e. the same amount of time regardless of the input array size. So its O (1) time and doesnt matter to our bigO analysis. The code inside the loop (lines 58) also takes constant time. So the running time of the code (as a function of n ) is determined by how many times the loop runs. To figure out how many times the loop runs, first suppose that n is a power of two, e.g. n = 2 k . Since the range starts at length n and is cut in half in each iteration, it will take k iterations before the range has length 1 and the loop stops. If you have to do such an analysis really fast, e.g. on an exam, you might be uncertain about whether the loop really runs k times or perhaps k + 1 or k 1 times. This difference doesnt matter: its still O ( k ) times. Since k = log 2 n , this means that the loop runs O (log n ) times....
View
Full
Document
 Spring '08
 Erickson
 Algorithms, Binary Search, Sort

Click to edit the document details