in page 54 the binary search is all messed up
1-high=mid+1 ----> high=mid-1
2-the final return is the one which will return the value for the main fn because anyway the program will excute it no matter the target was found or not
I edited the code and this one works...
Plz guys if anyone spotted or spots any mistakes in the any book post to avoid learning wrong stuff and i'd appreciate it if the mod makes a sticky thread about it.Code:int binsearch(int x, int v[], int n) { int low, high, mid,found; found=-1; low = 0; high = n - 1; while (low <= high && found==-1) { mid = (low+high)/2; if (x < v[mid]) high=mid-1; else if (x > v[mid]) low = mid + 1; else /* found match */ found=mid;} return (found); /* no match */ }



LinkBack URL
About LinkBacks


