二分查找

[code] #include

int bseek(int key) { int array[10] = {1,2,3,4,5,6,7,8,9,10}; int low = 0; int high = (sizeof(array) / 4) - 1; int mid = 0; int ret = -1;

    while(low <= high) {
            mid = (low + high) / 2;
            if (key == array[mid]) {
                    ret = mid;
                    break;
            } else if (key < array[mid]) {
                    high = mid - 1;
            } else if (key > array[mid]) {
                    low = mid + 1;
            }
    }

    return ret; }

void main(int argc, char* argv[]) { printf(“key[0]:%d\n”, bseek(0)); printf(“key[1]:%d\n”, bseek(1)); printf(“key[2]:%d\n”, bseek(2)); printf(“key[3]:%d\n”, bseek(3)); printf(“key[4]:%d\n”, bseek(4)); printf(“key[5]:%d\n”, bseek(5)); printf(“key[6]:%d\n”, bseek(6)); printf(“key[7]:%d\n”, bseek(7)); printf(“key[8]:%d\n”, bseek(8)); printf(“key[9]:%d\n”, bseek(9)); printf(“key[10]:%d\n”, bseek(10)); printf(“key[11]:%d\n”, bseek(11)); }

[/code]

[code] list = [1,2,3,4,5,6,7,8,9,10] low = 0 high = len(list) - 1 idx = 0

def bseek(key): global low global high global idx while(low <= high): mid = (low + high) / 2 if (key == list[mid]): idx = mid break elif(key < list[mid]): high = mid - 1 elif(key > list[mid]): low = mid + 1

bseek(6) print idx

[/code]

糖果

糖果
LUA从入门到放弃

Moonscript如何显示复选框

Moonscript如何显示复选框 Continue reading

Lapis框架的常用处理方法

Published on March 02, 2017