// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <malloc.h>
int _tmain(int argc, _TCHAR* argv[])
{
int num[5] = { 1, 3, 5, 7, 9 };
int* num1 = (int*)malloc(sizeof(int) * 6);
int* num2 = new int[6];
int key = 8;
int left = 0, right = 4;
int mid;
while (left < right)
{
mid = (left + right) / 2;
if (key == num[mid])
{
return mid;
}
else if (key < num[mid])
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
if (left == right)
{
for (int i = 0; i < mid+1; i++)
{
num2[i] = num[i];
}
num2[mid+1] = key;
for (int j = mid + 1; j < 5; j++)
{
num2[j + 1] = num[j];
}
}
for (int i = 0; i < 6; i++)
{
printf("%d\n", num2[i]);
}
return 0;
}
折半查找,插入
最新推荐文章于 2022-12-17 14:18:05 发布