This is an extremely simple algorithm. It is precisely because of its simplicity that I often get killed by it. I followed suit either during the competition or during the interview. Because of its simplicity, I don't want to spend too much time writing it. I always want to write it in one breath, but it doesn't work either. So my head is getting more and more chaotic, and my mood is getting more and more excited. As a result, the tragedy comes along.
Binary Search requires that the queried array be an ordered array, and its time complexity is O (log (N). That is to say, 0.1 billion of the data volume can be obtained after it is run for about 26 times.
# Include <stdio. h> # include <stdlib. h> int B _search (void * arr, int N, INT (* CMP) (void *, void *), void * Key, int size) {int left = 0, right = n-1, mid = 0; while (left <= right) {mid = (left + right)/2; If (CMP (Key, (char *) arr + mid * size) = 0) return mid; else if (CMP (Key, (char *) Arr + mid * size) <0) Right = mid-1; else left = Mid + 1;} return-1 ;}