#include<iostream>
#include<stdlib.h>
using namespace std;
// 折半查找并插入元素的函数
void Zhecha(int A[], int &n, int item) {
int low = 0, high = n - 1, mid;
// 折半查找循环
while (low <= high) {
mid = (low + high) / 2;
if (A[mid] == item) {
low = mid;
break;
}
else if (A[mid] > item) high = mid - 1;
else low = mid + 1;
}
// 向后移动元素为插入新元素腾出位置
for (int i = n - 1; i >= low; i--) {
A[i + 1] = A[i];
}
// 插入新元素
A[low] = item;
n = n + 1;
}
int main() {
// 定义测试数组
int A[] = {1, 3, 5, 7, 8, 9};
int n = 6;
// 调用函数插入元素 6
Zhecha(A, n, 6);
// 输出插入元素后的数组
for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}
return 0;
}