数据结构查找算法之折半查找

算法思想:

初始条件:

查找集合为整个有序表,通过查找不断缩小查找集合。

取有序表的中间记录作为比较对象,则

(1)若给定值与中间记录的关键码相等,则查找成功。

(2)若给定值小于中间记录的关键码,则在中间区域的左半区查找。

(3)若给定值大于中间记录的关键码,则在中间区域的的右半去查找。

不断重复上述过程,直到查找成功或查找失败(整个查找集合无记录)。

#include <stdio.h>
#include <string.h>

//折半查找递归算法
int BinSearchOne(char szChar[], int nLowerBound, int nUpperBound, char nSearchElement)
{
	//int nIndex = (nUpperBound + nLowerBound - 1) / 2;					(1 + 1 - 1) / 2 = 0 栈溢出
	int nIndex = (nUpperBound + nLowerBound + 1) / 2;
	char nMiddleElement = szChar[nIndex];            //取有序表的中间记录

	//2.递归过程处理
	if (nLowerBound > nUpperBound)			//查找失败,最后结束递归(整个查找集合中没有该元素)	
	{
		return -1;
	}

	else
	{
		//1.一个查找集合的处理过程
		if (nSearchElement < nMiddleElement)
		{
			//缩小查找集合
			nUpperBound = nIndex - 1;
			return BinSearchOne(szChar, nLowerBound, nUpperBound, nSearchElement);	 //必须使用此种方式返回主调函数的栈帧
		}

		else if (nSearchElement > nMiddleElement)
		{
			//缩小查找集合
			nLowerBound = nIndex + 1;
			return BinSearchOne(szChar, nLowerBound, nUpperBound, nSearchElement);		
		}

		else
		{
			return nIndex;								//查找成功,提前结束递归
		}
	}
}

//折半查找非递归算法
int BinSearchTwo(char szChar[], int nLowerBound, int nUpperBound, char nSearchElement)
{
	
	int nIndex = -1;
	char nMiddleElement = -1;							       //取有序表的中间记录

	//循环处理过程
	while (nLowerBound <= nUpperBound)                                                     //查找过程无需回退,一往无前
	{

		//1.第一个查找集合的处理过程
		nIndex = (nUpperBound + nLowerBound + 1) / 2;
		nMiddleElement = szChar[nIndex];

		if (nSearchElement < nMiddleElement)
		{
			//缩小查找集合
			nUpperBound = nIndex - 1;
			continue;
		}

		else if (nSearchElement > nMiddleElement)
		{
			//缩小查找集合
			nLowerBound = nIndex + 1;
			continue;		
		}

		else
		{
			return nIndex;							        //查找成功
		}
	}

	return -1;
}

int main()
{
	char szChar[] = "abcdefg";
	int nResult = -1;

	printf("****折半查找递归算法****\n");
	for (unsigned int i = 0; i <= strlen(szChar); i++)
	{
		nResult = BinSearchOne(szChar, 0, strlen(szChar) - 1, i + 'a');

		if (nResult == -1)
		{
			printf("顺序表中不存在元素:%c.\n", i + 'a');
		}

		else
		{
			printf("元素:%c 在顺序表中的下标:%d.\n", i + 'a', nResult);
		}
	}
	
	printf("\n****折半查找非递归算法****\n");
	for (i = 0; i <= strlen(szChar); i++)
	{
		nResult = BinSearchTwo(szChar, 0, strlen(szChar) - 1, i + 'a');

		if (nResult == -1)
		{
			printf("顺序表中不存在元素:%c.\n", i + 'a');
		}

		else
		{
			printf("元素:%c 在顺序表中的下标:%d.\n", i + 'a', nResult);
		}
	}

	return 0;
}

运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值