11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 9.1 Searching Unsorted <strong>and</strong> Sorted Arrays 305she will first open the dictionary about three quarters of the way through <strong>and</strong> thenmake a decision based on wh<strong>at</strong> is found as to where to look next. In other words,people typically use some knowledge about the expected distribution of key valuesto “compute” where to look next. This form of “computed” binary search is calleda dictionary search or interpol<strong>at</strong>ion search. In a dictionary search, we search L<strong>at</strong> a position p th<strong>at</strong> is appropri<strong>at</strong>e to the value of K as follows.p =K − L[1]L[n] − L[1]This equ<strong>at</strong>ion is computing the position of K as a fraction of the distance betweenthe smallest <strong>and</strong> largest key values. This will next be transl<strong>at</strong>ed into th<strong>at</strong>position which is the same fraction of the way through the array, <strong>and</strong> this positionis checked first. As with binary search, the value of the key found elimin<strong>at</strong>es allrecords either above or below th<strong>at</strong> position. The actual value of the key found canthen be used to compute a new position within the remaining range of the array.The next check is made based on the new comput<strong>at</strong>ion. This proceeds until eitherthe desired record is found, or the array is narrowed until no records are left.A vari<strong>at</strong>ion on dictionary search is known as Quadr<strong>at</strong>ic Binary Search (QBS),<strong>and</strong> we will analyze this in detail because its analysis is easier than th<strong>at</strong> of thegeneral dictionary search. QBS will first compute p <strong>and</strong> then examine L[⌈pn⌉]. IfK < L[⌈pn⌉] then QBS will sequentially probe to the left by steps of size √ n, th<strong>at</strong>is, we step throughL[⌈pn − i √ n⌉], i = 1, 2, 3, ...until we reach a value less than or equal to K. Similarly for K > L[⌈pn⌉] we willstep to the right by √ n until we reach a value in L th<strong>at</strong> is gre<strong>at</strong>er than K. We arenow within √ n positions of K. Assume (for now) th<strong>at</strong> it takes a constant number ofcomparisons to bracket K within a sublist of size √ n. We then take this sublist <strong>and</strong>repe<strong>at</strong> the process recursively. Th<strong>at</strong> is, <strong>at</strong> the next level we compute an interpol<strong>at</strong>ionto start somewhere in the subarray. We then step to the left or right (as appropri<strong>at</strong>e)by steps of size √ √ n.Wh<strong>at</strong> is the cost for QBS? Note th<strong>at</strong> √ c n = c n/2 , <strong>and</strong> we will be repe<strong>at</strong>edlytaking square roots of the current sublist size until we find the item th<strong>at</strong> we arelooking for. Because n = 2 log n <strong>and</strong> we can cut log n in half only log log n times,the cost is Θ(log log n) if the number of probes on jump search is constant.Say th<strong>at</strong> the number of comparisons needed is i, in which case the cost is i(since we have to do i comparisons). If P i is the probability of needing exactly iprobes, then√∑ niP(need exactly i probes)i=1= 1P 1 + 2P 2 + 3P 3 + · · · + √ nP √ n

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!