顺序查找和折半查找

/****************************************************
      [实验目的]
          实现顺序查找和折半查找,
          对两种查找方法作比较
      [实验提示]
          1. 编写顺序查找和折半查找算法
          2. 修改算法把查找过程中所作比较及其结果
             打印出来,观察并对比查找过程
          3. 分析并计算两种算法的平均查找长度
*****************************************************/

#include <stdio.h>
#include <stdlib.h>

#define ElemType int

//顺序查找
int SqSearch(ElemType a[],int n,ElemType x);
int SqSearch2(ElemType a[],int n,ElemType x); //打印查找过程 
//折半查找
int BinSearch(ElemType a[],int n,ElemType x);
int BinSearch2(ElemType a[],int n,ElemType x); //打印查找过程
//打印数组数据
void PrintArray(ElemType a[],int n);

int main()
{
	int i,x;
	//测试数据
	const  int N = 9;
	ElemType a1[N+1]={0,34,23,12,56,90,78,89,45,67}; //0号单元留空
	ElemType a2[N+1]={0,12,23,34,45,56,67,78,89,90}; //0号单元留空

	//顺序查找
	printf("\n顺序查找\n");
	printf("a1[]="); 
	PrintArray(a1,N);
	
	printf("\n输入要查找的数据: ");
	scanf("%d",&x);
	
	if((i=SqSearch(a1,N,x))>0) //找到 
		printf("\n找到 x==a1[%d]\n",i);
	else //未找到 
		printf("\n找不到 %d \n",x);
	
	printf("\n查找过程:\n");
	SqSearch2(a1,N,x);
	printf("\n完成\n");

	//折半查找
	printf("\n折半查找\n");
	printf("a2[]=");
	PrintArray(a2,N);
	
	printf("\n输入要查找的数据: ");
	scanf("%d",&x);
	
	if((i=BinSearch(a2,N,x))>0) //找到 
		printf("\n找到 x==a2[%d]\n",i);
	else //未找到
		printf("\n找不到 %d \n",x);
	
	printf("\n查找过程:\n");
	BinSearch2(a2,N,x);
	printf("\n完成\n");

	system("pause");
	return 0;
}

//在数组a[1..n]中顺序查找x
//  找到时返回元素下标,否则返回0 
int SqSearch(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 顺序查找算法
	for(int i=1;i<=n;i++){
		if(a[i]==x) return i;
	}
    
    //-------------------------------------
	return 0; //找不到 
}

int SqSearch2(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 顺序查找算法,打印每次比较结果 
	for(int i=1;i<=n;i++){
		if(a[i]!=x){ 
			printf("%d",i);
		    PrintArray(a,i);
		}
		else break;
		
	}
    
    //-------------------------------------
	return 0;
}

//在数组a[1..n]中折半查找x
//  找到时返回元素下标,否则返回0 
//前提:a[1..n]是非递减有序的 
int BinSearch(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 折半查找算法
	int mid,low=1,high=n;
	while(low<high){
		mid=(low+high)/2;
		if(a[mid]==x) return mid;
		else if(a[mid]>x) high=mid--;
		     else low=mid++;

	}
    
    //-------------------------------------
	return 0; //找不到 
}

int BinSearch2(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 折半查找算法,打印每次比较结果
    int mid,low=1,high=n;
	while(low<high){
		mid=(low+high)/2;
		if(a[mid]==x) return mid;
		else if(a[mid]>x) {high=mid-1;printf("%d\n",a[mid]);}
		else {low=mid+1;printf("%d\n",a[mid]);};

	}
    
    //-------------------------------------
	return 0;
}

//打印数组数据a[1..n]
void PrintArray(int a[],int n)
{
	int i;
	printf("{ ");
	for(i=1; i<=n; i++)
		printf("%d ",a[i]);
	printf("}\n");
}

实验结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

beyond_LH

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值