#include <iostream>
#include <stdio.h>
using namespace std;
bool searchSortedArray(int searchKey, int arr[], int arrLen) {
int low = 0;
int high = arrLen - 1;
bool exist = false;
while(low <= high) {
int mid = low + ((high - low) >> 1);
if(searchKey == arr[mid]) {
exist = true;
break;
}
else if(searchKey > arr[mid]) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return exist;
}
void insertSort(int arr[], int n) {
if (n==0) {
printf("数组为空\n");
return;
}
for(int orderedNum = 1; orderedNum < n; orderedNum++) {
int sortedLastIndex = orderedNum - 1;
int sortNum = arr[orderedNum];
for(; sortedLastIndex >= 0; sortedLastIndex--) {
if(arr[sortedLastIndex] > sortNum) {
arr[sortedLastIndex + 1] = arr[sortedLastIndex];
} else {
break;
}
}
arr[sortedLastIndex + 1] = sortNum;
}
}
int main() {
int arr[] = {85, 63, 52, 34, 25, 17, 6};
insertSort(arr, 7);
int searchKey = 6;
if(searchSortedArray(searchKey, arr, 7)) {
printf("%d存在于数组中\n", searchKey);
}
else {
printf("%d不存在于数组中\n", searchKey);
}
return 0;
}