折半查找算法的使用

维基上的代码:

int binary_search(int A[], int key, int imin, int imax)
{
  // continue searching while [imin,imax] is not empty
  while (imax >= imin)
    {
      // calculate the midpoint for roughly equal partition
      int imid = midpoint(imin, imax); 
      if(A[imid] == key)
        // key found at index imid
        return imid; 
      // determine which subarray to search
      else if (A[imid] < key)
        // change min index to search upper subarray
        imin = imid + 1;
      else         
        // change max index to search lower subarray
        imax = imid - 1;
    }
  // key was not found
  return KEY_NOT_FOUND;
}

评估下面两个算法:

int binary_search()
{
    int size ;
    char keys[] = "9290";
    char words[][6] ={"1","2","21","22","222","3","5","66","90","900","91","929"};
	size = sizeof(words) / sizeof(words[0]);
	printf("第一个算法 sizeof = %d\n", size);
    int head    = 0;
    int tail    = size;
	int cursor = (head + tail) / 2; 
	int result = 999;
	int count = 0;
	printf("keys= \"%s\"\n", keys);
	printf("words[i][] = \n");
	for (int i = 0; i < size; ++i) printf("[%d]=\"%s\" ", i, words[i]);

    do
    {
        result = strcmp(keys, words[cursor]);
		printf("\ncount = %d  cursor = %d head = %d , tail = %d , \n", count++ ,cursor, head, tail);
		printf("words[%d] = \"%s\" , result = %d \n\n ", cursor, words[cursor] ,result);
        if(result > 0)
        {
            head = cursor;
        }
        else if (result < 0)
        {
            tail = cursor;
        }
		else
		{
			break;//找到匹配的key按键组合
		}
		cursor = (head + tail) / 2;

    }
    while((head < cursor) && (cursor <= tail)); //((head <= cursor) && (cursor < tail))


	printf("\n------------------\ncursor = %d head = %d , tail = %d , \n", cursor, head, tail);
	printf("words[%d] = \"%s\" , result = %d \n\n", cursor, words[cursor], result);


	printf("====================\n");

	int left = 0, right = size - 1, middle = 0; //, result = 0;

	count = 0;
	printf("第二个算法 sizeof = %d\n", size);
	printf("keys= \"%s\"\n", keys);
	printf("words[i][] = \n");
	for (int i = 0; i < size; ++i) printf("[%d]=\"%s\" ", i, words[i]);

	printf("\nmiddle = %d, left = %d , right = %d , \n", middle, left, right);
	while (left <= right)
	{
		middle = (left + right) >> 1;
		result = strcmp(keys, words[middle]);
		printf("count = %d middle = %d, left = %d , right = %d , \n", count++, middle , left, right);
		printf("words[%d] = \"%s\" , result = %d \n\n", middle , words[middle], result);
		if (result > 0)
		{
			left = middle + 1;
		}
		else if (result < 0) right = middle - 1;
		else break;
	}
	printf("---------------------\nmiddle = %d, left = %d , right = %d , \n", middle, left, right);
	printf("words[%d] = \"%s\" , result = %d \n ", middle , words[middle], result);

    return cursor;

}

程序结果:

第一个算法 sizeof = 12
keys= "11"
words[i][] =
[0]="1" [1]="2" [2]="21" [3]="22" [4]="222" [5]="3" [6]="5" [7]="66" [8]="90" [9]="900" [10]="91" [11]="929"
count = 0  cursor = 6 head = 0 , tail = 12 ,
words[6] = "5" , result = -1

count = 1  cursor = 3 head = 0 , tail = 6 ,
words[3] = "22" , result = -1

count = 2  cursor = 1 head = 0 , tail = 3 ,
words[1] = "2" , result = -1

------------------
cursor = 0 head = 0 , tail = 1 ,
words[0] = "1" , result = -1


====================
第二个算法 sizeof = 12
keys= "11"
words[i][] =
[0]="1" [1]="2" [2]="21" [3]="22" [4]="222" [5]="3" [6]="5" [7]="66" [8]="90" [9
]="900" [10]="91" [11]="929"
middle = 0, left = 0 , right = 11 ,
count = 0 middle = 5, left = 0 , right = 11 ,
words[5] = "3" , result = -1

count = 1 middle = 2, left = 0 , right = 4 ,
words[2] = "21" , result = -1

count = 2 middle = 0, left = 0 , right = 1 ,
words[0] = "1" , result = 1

count = 3 middle = 1, left = 1 , right = 1 ,
words[1] = "2" , result = -1

---------------------
middle = 1, left = 1 , right = 0 ,
words[1] = "2" , result = -1
 请按任意键继续. . .


当搜索失败的时候,“中间点”停留在哪里呢?能否停留在“最近似”的一个位置?忙于工作,有机会再研究。

posted on 2015-02-13 14:27  johnphan  阅读(176)  评论(0编辑  收藏  举报

导航