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

📄 main.c

📁 使用VC6.0对C语言折半查找算法的 实现
💻 C
字号:
#include<stdio.h>

void maopao(int tempArray[],int index)//冒泡排序
{int i,j,temp;
 
 
 for(i=index;i>0;i--)
 {
	 for(j=0;j<i;j++)
	 {
		 if(tempArray[j]>tempArray[j+1])
		 {
			 temp=tempArray[j];
		  tempArray[j]=tempArray[j+1];
		  tempArray[j+1]=temp;
		 }
	 }
	 
 }
}

void shunxu(int tempArray[],int index,int x)//顺序查找法
{int i;
int num=-1;
 for(i=0;i<index;i++)
 {
	 if(tempArray[i]==x)
	 {
		 num=i;
	 }
 }
 if(num!=-1)
	 printf("\n该数据为数组第%d个数据 data[%d]=%d \n",num+1,num,x);
 else
     printf("\n未找到该数据!\n");
}


void zheban(int tempArray[],int index,int x)//折半查找,二分查找
{int low=0,high=index-1,mid,num=-1;
  do
  {
	  mid=(low+high)/2;
	  if(x==tempArray[mid])
	  {
         num=mid;
		 break;
	  }
	  if(x>tempArray[mid])
	  {
		  low=mid+1;
	  }
	  else
	  {
		  high=mid-1;
	  }

  }while(low<=high);

 if(num!=-1)
	 printf("\n该数据为数组第%d个数据 data[%d]=%d \n",num+1,num,x);
 else
     printf("\n未找到该数据!\n");
}
 
void main()
{int data[25]={2,5,6,9,8,10,11,22,33,45,58,67,76,99,17,21,32,56,96,68,77,13,35,85,81};
 int i,m,n;
 n=sizeof(data)/sizeof(int);
 printf("数组含有%d个元素!\n",n);
 printf("\n显示排序前数组数据:\n");
 for(i=0;i<25;i++)
 {printf(" %d  ",data[i]);
 }
 printf("\n冒泡排序:\n");
 maopao(data,n);

 printf("\n显示排序后数组数据:\n");
 for(i=0;i<25;i++)
 {printf(" %d  ",data[i]);
 }
 printf("\n输入要查找的数据:\n");
	 scanf("%d",&m);
 printf(" 使用顺序查找法!\n");
 shunxu(data,n,m);

 printf(" ***************************\n");
printf(" 使用折半查找法!\n");
zheban(data,n,m);
}

⌨️ 快捷键说明

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