📄 search.c
字号:
#include "stdio.h"
#include "conio.h"
#define N 10
int E[N]={23,25,39,14,45,78,47,19,35,124};
void ss_sort(int e[],int n)
{
int i,j,k,t;
for(i=0;i<n-1;i++){
for(k=i,j=i+1;j<n;j++)
if(e[k]>e[j])
k=j;
if(k!=i)
{t=e[i];e[i]=e[k];e[k]=t;}
}
}
int search(int *k,int n,int key)
{
int i;
for(i=0;i<n&&key>k[i];i++)
;
if(i<n&&k[i]==key)
return i;
return -1;
}
int bin_search(int *k,int n,int key)
{
int low=0,high=n-1,mid;
while(low<=high){
mid=(low+high)/2;
if(key==k[mid])
return mid;
if(key>k[mid])
low=mid+1;
else
low=mid-1;
}
return -1;
}
void main()
{
int i,j;
printf("The old_number'list is :\n ");
for(i=0;i<N;i++)
printf("%d ",E[i]);
ss_sort(E,N);
printf("\nNew listed number is :\n");
for(i=0;i<N;i++)
printf("%d ",E[i]);
printf("\nPlease enter the key number which you want to search:\n");
scanf("%d",&i);
if((j=search(E,N,i))>=0)
printf("Find the key number,the seat is %d\n",j+1);
else
printf("Not find the key number.\n");
getch();
printf("\nPlease enter the key number which you want to search:");
scanf("%d",&i);
if((j=bin_search(E,N,i))>=0)
printf("Find the key number,the seat is %d\n",j+1);
else
printf("Not find the key number.\n");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -