int array[10] = {14,78, 67, 12,37, 92, 54,26, 31, 14};
BOOL neednext = YES;//优化的选择排序
for (int i = 0; i < 10 && neednext; i++) {
neednext = NO;
for (int j =0; j < 9 - i; j++) {
if (array[j] > array[j + 1]) {
neednext = YES;
int temp =array[j];
array[j] =array[j + 1];
array[j + 1] = temp;
}
}
}
for (int i = 0; i < 10; i++) {
printf("%d\t", array[i]);
}
//折半查找二分法查找
int start = 0;
int end = 9;
int mid = (start + end) / 2 ;
int number = 78;//
while (array[mid] != number && start < end) {
if (array[mid] > number ) {
end = mid - 1;
}else
start = mid + 1;
mid = (start + end) / 2;
}
if (array[mid] == number) {
printf("%d\n", mid);
}else
printf("此数不在数组中");