# 实验6.rar

• sosorry
了解作者
• C/C++
开发工具
• 4KB
文件大小
• rar
文件格式
• 0
收藏次数
• 1 积分
下载积分
• 0
下载次数
• 2019-01-10 00:23
上传日期

• main.plg
744B
• main.ncb
33KB
• main.dsw
516B
• main.c
1.3KB
• main.opt
47.5KB
• main.dsp
3.3KB

#include <stdio.h> int a[101],n; void quicksort(int left, int right) { int i, j, t, temp; if(left > right) return; temp = a[left]; i = left; j = right; while(i != j) { while(a[j] >= temp && i < j) j--; while(a[i] <= temp && i < j) i++; if(i < j) { t = a[i]; a[i] = a[j]; a[j] = t; } } a[left] = a[i]; a[i] = temp; quicksort(left, i-1); quicksort(i+1, right); } int search(int a[],int x,int n) { int low=0,high=n-1,mid; while(low<=high) { mid=(low+high)/2; if(a[mid]==x) return mid; else if(x<a[mid]) high=mid-1; else low=mid+1; } return -1; } int main() { int i,k,j; printf("Please input quantity of Key :\n"); scanf("%d", &n); printf("Please input each key :\n"); for(i = 0; i < n; i++) scanf("%d", &a[i]); quicksort(0, n-1); for(i = 0; i <n; i++) printf("%d ", a[i]); printf("\n"); printf("Please input Key to find :\n"); scanf("%d",&k); j=search(a,k,n); if(j!=-1) printf("Found at %d.\n",j); else printf("Not found!\n"); printf("Please input another Key to find :\n"); scanf("%d",&k); j=search(a,k,n); if(j!=-1) printf("Found at %d.\n",j); else printf("Not found!\n"); return 1; }

相关推荐