C语言复习之两种常用检索方式

【每天进步一点点,让优秀成为一种习惯】

今天的心情比较糟糕。一直以来心情都不错,感觉大学里边交到了不少很好的朋友,也算是读大学最大的收获。

好了,吐槽完毕,下面介绍两种常用的检索方式:顺序检索和对半检索~

具体算法不介绍了,可以google or baidu。直接上源码:

//designed by yjysdu
//20120304
#include<stdio.h>
#define  n 10
int search(int key,int a[])
{
	int i;
	
	for (i=0;i<10;i++)
	{
		if (a[i]==key)
			return i;
	}
	return -1;
}


int half_search(int key,int a[])
{
	int low = 0;
	int high = n-1;
	int i=0;
	while (high-low>0)
	{
		if (a[i]==key)
			return i;
		i++;
	}
	return -1;
}


void main(void)
{
	
    int	a[n] ={1,2,3,4,5,6,7,8,9,10}; 
	int key = 8;
	int output1,output2;

	output1 = search(key,a);
	printf("%d\n",output1);
	output2 = half_search(key,a);
	printf("%d\n",output1);

}


版权声明:本文为forest_1252原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。