//测试主函数 #include<stdio.h> #include<stdlib.h> #include<math.h> #define MAX_SIZE 1000 voidselect_swap(int *a, int *b);//实现代码见上一篇:选择排序算法 voidsort(intlist[], int n);//实现代码见上一篇:选择排序算法 intcompare(int a, int b); intbinsearch(intlist[], int searchnum, int left, int right); intmain() { int num; intlist[MAX_SIZE]; printf("Enter the number of numbers to generate:"); scanf_s("%d", &num); if (num<1 || num>MAX_SIZE) { fprintf(stderr, "Improper value of n.\n"); exit(1); } for (int i = 0; i < num; i++) { list[i] = rand() % 1000; printf("%d ", list[i]); } sort(list, num); printf("\nsorted array:\n"); for (int i = 0; i < num; i++) printf("%d ", list[i]); printf("\ninput searchnum:"); int searchnum; scanf_s("%d", &searchnum); int indice = binsearch(list, searchnum, 0, num-1); indice>-1 ? printf("%d is in the sorted list:list[%d]=%d\n", searchnum,indice, searchnum) : printf("%d is not in the sorted list.\n", searchnum); system("pause"); return0; }