C-Note: Find an array element

Original link: http://c.biancheng.net/c/

Conventional search method (sequential search)

Sequential search is the individual data of unknown origin in a certain order and compare data to see if there is equal to the data you want to find the data, the results look, there are two: Find success with the lookup fails

#include <stdio.h>
#define N 10
void Input(int a[],int n);//杈撳叆鏁扮粍鐨刵涓厓绱?
int Search(int a[],int n,int x);
int main(void)
{
	int a[N];
	printf("输入数组a的%d个元素:\n",N);
	Input(a,N);
	printf("请输入要查找的元素x:");
	int x;
	scanf("%d",&x);//输入要查找的元素x
	int res=Search(a,N,x);
	if(res!=-1)
	{
		printf("查找%d成功,它的序号为:%d\n",x,res);
	}
	else
	{
		printf("查找%d失败\n");
	}
	return 0;
}
void Input(int a[],int n)
{
	int i;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
}
int Search(int a[],int n,int x)
{
	int i=0;
	while(i<n)
	{
		if(a[i]==x)
		{
			break;
		}
		i++;
	}
	if(i<n)
	{
		return i+1;//鏌ユ壘鎴愬姛锛岃繑鍥炲叾搴忓垪鍙凤紙浠?寮€濮嬶級
	}
	return -1;
}

Output:
Here Insert Picture Description

Binary search (binary search)

Previous sequential search process for any set of data can be used, and the disadvantage is the need to compare all the elements, when more data is inefficient.
Binary search to find the premise first requires the n data elements must be ordered (assumed to be in ascending order), as the data is out of order, the first sorted by a sorting algorithm.

Algorithm Description

Suppose you want to find an element is x, the first element of the index to find the range of low, subscript final element is high, if you want to find in the range of at least one element (i.e., low <= high), the intermediate element subscript mid = (low + high) / 2, Comparative x relationship between elements and mid corresponds, if x is equal to a [mid] the query succeeds, the return mid + 1; if x is smaller than a [mid], if the data and x corresponding to the presence element, it is certainly index between low ~ mid-1 (performing high = mid-1); if x is greater than a [mid], if the x corresponding to the data elements present in it subscript affirmative (performing low = mid + 1), refolded and between mid + 1 high, lookup. Until x == a [mid] is true or not find the range of the element (low <= high false)

Implementation code

#include <stdio.h>
#define N 6//数组元素个数 
void Input(int a[],int n);//输入数组的n个元素
void Output(int a[],int n);//输出数组的n个元素
void BubbleSort(int a[],int n);//对有n个元素的数组进行冒泡排序
int BinSearch(int a[],int n,int x);//在数组a中使用折半查找法查找x 
void Swap(int *pa , int *pb);
int main(void)
{
	int a[N],x;
	printf("请输入数组的%d个元素:\n",N);
	Input(a,N);
	BubbleSort(a,N);
	printf("排序后的数组a的元素为:\n");
	Output(a,N);
	printf("输入要查找的元素x:");
	scanf("%d",&x);
	int t=BinSearch(a,N,x);//用折半查找法查找x
	if(t!=0)
	{
		printf("查找%d成功,序号为%d\n",x,t);	
	}
	else
	{
		printf("查找%d失败!\n",x);
	}
	return 0;	
} 
void Input(int a[],int n)
{
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
}
void Output(int a[],int n)
{
	for(int i=0;i<n;i++)
	{
		printf("%-4d",a[i]);
	}
	printf("\n");
}
void BubbleSort(int a[],int n)
{
	for(int i=1;i<n;i++)//轮数 
	{
		for(int j=0;j<=n-1-i;j++)
		{
			if(a[j]>a[j+1])
			{
				Swap(&a[j],&a[j+1]); 
			}
		}
	} 
}
int BinSearch(int a[],int n,int x)
{
	int low,high,mid;
	low=0;high=n-1;
	while(low<=high)//查找条件是区间中至少有一个元素
	{
		mid=(low+high)/2;//计算查找区间中间元素的下标
		if(x==a[mid])
		{
			return mid+1;//mid对应的元素正好为要查找的元素,返回下标 
		} 
		else if(x<a[mid])
		{
			high=mid-1;//比中间元素小,则需要在左区间继续查找 
		}
		else
		{
			low=mid+1;//比中间元素大,则需要在右区间继续查找 
		}
	} 
}
void Swap(int *pa , int *pb)
{
	int temp;
	temp=*pa;
	*pa=*pb;
	*pb=temp; 
}

Output:

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_42124234/article/details/102536568