Bubble sort method (array method)

#define M 10//宏来控制数组的大小
#include<stdio.h>
int main()
{
    
    
 int a[M] = {
    
    -12,0,6,16,23,56,80,100,110,115};//定义并初始化数组
 int n, low, mid, high,found;
 low = 0;
 high = M-1;
 found = 0;
 printf("Please input a number:\n");
 scanf("%d",&n);//从键盘输入需要查找的数
 if(n <a[0]||n >a[M-1])
 while(low<=high)
 {
    
    
  mid = (low + high)/2;
  if(n == a[mid])
  {
    
    
   found = 1;//用于判断是否查找到该数据
   break;
  }
  else if(n>a[mid])
  {
    
    
   low = mid + 1;
  }
  else
  {
    
    
   high = mid - 1;
  }
 }
 if(found == 1)
 {
    
    
  printf("The index of %d is %d\n",n,mid);//n这个数位于数组第几
 }
 else
 {
    
    
  printf("There is no %d...\n",n);//数组中不存在该数
 }
 return 0;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45824959/article/details/105102525