c++ - indices examined during binary search -
i'm going through c++ book , have question binary search. there array called critter
filled (in order) = {auk, bat, cow, eel, elk, fox, gnu, pig, rat}
so if wanted find eel, believe go through indices 4 -> 1 -> 2 -> 3 formula given in book. if want search not in array?
the book tells me algorithm is
set found = false set first = 0 set lastly = n - 1 (n beingness number of elements) while first <= lastly , !found following: a) calc loc = (first+last)/2 b) if item < a[loc] set lastly = loc - 1 else if a[loc] > item set first = loc + 1 else set found = true end while
so if item not there, jump downwards found = true, since can't < or >? or go though every index in array?
you notice loop exit when first
not smaller last
: while first <= last
.
when happens before found set true homecoming false.
c++ binary-search
No comments:
Post a Comment