标签:tchar code include bre lse print style color span
#include <stdio.h> #include <stdlib.h> //先进行排序,在进行查找 void sort(int arr[10]) { int i,j,idx; for(i=0;i<9;i++) { idx =i; for(j=i+1;j<10;j++) { if(arr[idx]>arr[j]) idx = j; } if(idx!=i) { int temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } } } int find(int *arr,int findData) { int low=0,mid,high=9; int idx=-1; while(low<=high) { mid = (low+high)/2; if(arr[mid]==findData) { idx = mid;break; } //找的数比中间值小,不要了,包括中间值,变更最高位,减一除去了mid值 else if(arr[mid]>findData) high = mid - 1; else if(arr[mid]<findData) low = mid + 1; } return idx; } void print(int arr[10]) { for(int i=0;i<10;i++) { printf("%d\n",arr[i]); } } void main() { int arr[10]; int findData,idx; for(int i=0;i<10;i++) { arr[i] = rand()%100; } print(arr); sort(arr); putchar(10); print(arr); scanf("%d",&findData); idx = find(arr,findData); if(idx!=-1) printf("I find out,it‘s arr[%d]",idx); else printf("sorry,I can‘t do it"); system("pause"); }
标签:tchar code include bre lse print style color span
原文地址:https://www.cnblogs.com/huxiaobai/p/10295669.html