Friday 15 April 2011

c - Making use of binary search algorithm -



c - Making use of binary search algorithm -

i'm more interested in thought process in code itself. i'm supposed find number of v[i] components in sorted array such x < v[i] < y, x , y sent input keyboard. i'm supposed solve efficiently using modified binary search instead of regular search through vector.

my problem can't visualize how implement binary search in case, method doesn't seem fit me.

any thoughts ?

you binary search in array x , y. subtract index of x index of y how many items between them.

you'd have subtract 1 result since using strictly less-than.

for example, have array:

[3, 6, 8, 10, 14, 39, 41, 100]

let x=8 , y = 39.

the index of x 2 , index of y 5.

5-2 = 3

3-1 = 2

if x , y allowed values not contained in array, can still follow same approach. search x , if not found, utilize index of element larger x. likewise index of element smaller y.

c algorithm

No comments:

Post a Comment