⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 twodfind.cpp

📁 该程序主要用于二分法算法的实现,能够进行数据查找.
💻 CPP
字号:
#include"iostream.h"
const size=5;

main()
{
	int i,j;
	float t,a[size];

	for(i=0;i<size;i++)
	{
		cout<<"a["<<i<<"]=";
		cin>>a[i];
	}
	//对数组从小到大顺序排序
	for(i=0;i<=size-1;i++)
		for(j=i+1;j<size;j++)
			if(a[i]>a[j])
			{
				t=a[i];
				a[i]=a[j];
				a[j]=t;
			}

	//显示排序结果
	for(i=0;i<size;i++)
		cout<<a[i]<<" ";
	cout<<endl;

	//输入要查找的数据
	int value;
	int found;  //find 1, don't find 0;
    int low,high,mid;

	cout<<"value=";
	cin>>value;
	

    //用二分法查找数组
	found=0;
	low=0;
	high=size-1;
	while (low<high) 
	{
		mid=(high+low)/2;
		if (a[mid]==value) 
		{
			found=1;
			break;
		}
		if (a[mid]<value)
		{
			low=mid+1;
		}
		else
		{
			high=mid-1;
		}
	}
	if(found)
		cout<<"The value found at:a["<<mid<<"]="<<a[mid]<<endl;
	else
		cout<<"The "<<value<<" is not found!"<<endl;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -