码迷,mamicode.com
首页 > 编程语言 > 详细

折半查找算法的使用中防止溢出的问题

时间:2015-02-13 14:49:10      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:折半查找 查找算法 二分搜索

维基上的代码:

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= "9290"
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 = 9 head = 6 , tail = 12 ,
words[9] = "900" , result = 1

count = 2  cursor = 10 head = 9 , tail = 12 ,
words[10] = "91" , result = 1

count = 3  cursor = 11 head = 10 , tail = 12 ,
words[11] = "929" , result = 1

------------------
cursor = 11 head = 11 , tail = 12 ,
words[11] = "929" , result = 1


====================
第二个算法 sizeof = 12
keys= "9290"
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 = 12 ,
count = 0 middle = 6, left = 0 , right = 12 ,
words[6] = "5" , result = 1


count = 1 middle = 9, left = 7 , right = 12 ,
words[9] = "900" , result = 1


count = 2 middle = 11, left = 10 , right = 12 ,
words[11] = "929" , result = 1


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


忙于工作,有机会再研究效率的问题。

折半查找算法的使用中防止溢出的问题

标签:折半查找 查找算法 二分搜索

原文地址:http://blog.csdn.net/johnphan/article/details/43793759

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!