查找——实现监视哨法查找 #include<stdio.h> #include<stdlib.h> int cmp( const void *a, const void *b) { return *( int *)a-*( int *)b; } int main() { int a[ 101],i; for(i= 1;i<= 100;i++) a[i]=rand()% 100; for(i= 1;i<= 100;i++) printf( " %d ",a[i]); printf( " \n "); int key; scanf( " %d ",&key); printf( " 顺序查找\n "); a[ 0]=key; for(i= 100;i>= 0;i--) if(a[i]==key){ printf( " %d\n ",i); break; } qsort(&a[ 1], 100, sizeof( int),cmp); for(i= 1;i<= 100;i++) printf( " %d ",a[i]); printf( " \n "); printf( " 折半查找\n "); int mid,start= 1,end= 100; mid=(start+end)/ 2; while(start<=end) { if(a[mid]==key){ printf( " %d\n ",mid); break; } if(a[mid]>key){ end=mid- 1; mid=(end+start)/ 2; } else{ start=mid+ 1; mid=(start+end)/ 2; } } printf( " end\n "); }