lect_24 - Recursive Algorithms Margaret M Fleck 19 March...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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. Let’s look at this algo- rithm in more detail. In many applications, it’s sensible to assume that the input array is sorted. That is a 1 ≤ a 2 ≤ . . . ≤ a n . We’ll see that it would take O ( n log n ) time to sort an unsorted array. But sometimes an array of data comes pre-sorted. And sometimes an application will do many searches on an array of data that’s constant or changes only very slowly, e.g. the registrar’s 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. Let’s 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 it’s smaller than b , we know that b must live in the upper half of the range (if it’s in the array at all). Otherwise, b must live in the lower half of the range (again, if it’s in the array). 2 Analysis of binary search To do a big-O analysis of binary search’s running time, notice that the code in lines 1-3 and 9-10 takes constant time, i.e. the same amount of time regardless of the input array size. So it’s O (1) time and doesn’t matter to our big-O analysis. The code inside the loop (lines 5-8) 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 doesn’t matter: it’s still O ( k ) times. Since k = log 2 n , this means that the loop runs O (log n ) times....
View Full Document

{[ snackBarMessage ]}

Page1 / 7

lect_24 - Recursive Algorithms Margaret M Fleck 19 March...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online