JobHunting — 二分查找

五月 18th, 2011 by klose | No Comments | Filed in 求职技术, 编程点滴

算法不多说了,相信如果有人读过Jon Bentley 的《Programming Pearls》,就会十分重视这个算法了。记得上大学的时候,写的第一个程序也是这个,:-)。 #include <stdio.h> #include <stdlib.h> typedef int DataType; int length = 10; int data[] = {1, 3, 5, 23, 45, 231, 234, 754, 5632, 12345}; int bS(DataType * d, int length, DataType target) { int left = 0, right = length-1, mid; if    (length <= 0) { return -1; } while (left <= right) { mid = (right + left) >> 1; if (d[mid] > target) { right = mid – 1; } else if (d[mid] == target) { return mid; } else { left = mid + 1; } } return -1; } int main (int argc, char* argv[]) { if (argc != 2) { printf(“Usage:binarySear

Tags: ,