Ai
1 Star 0 Fork 0

ubuntuvim/algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bin_insertSort.c 1008 Bytes
一键复制 编辑 原始数据 按行查看 历史
ubuntuvim 提交于 2015-07-15 10:58 +08:00 . save to git...
#include <stdlib.h>
#include <stdio.h>
void bin_insertSort(int array[], int n)
{
int i, j, temp, low, high, mid;
for(i = 1; i < n; ++i)
{
temp = array[i]; //将当前需要被插入元素保存在temp中
low = 0;
high = i - 1;
while(low <= high) //利用折半查找确定被插入元素array[i]的合适位置
{
mid = (low + high) / 2;
if(temp < array[mid])
high = mid - 1;
else
low = mid + 1;
}
for(j = i - 1; j >= low; --j) //找到合适位置low, 将[low, i-1]中的元素依次后移一位
array[j + 1] = array[j];
array[low] = temp; //插入元素array[i], 至此插入排序的一趟结束
}
}
int main()
{
int array[] = { 6, 3, 0, 6, 5, 8, 7, 4, 2, 7 };
int n = sizeof(array) / sizeof(int);
bin_insertSort(array, n);
for(int i = 0; i < n; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ubuntuvim/algorithm.git
git@gitee.com:ubuntuvim/algorithm.git
ubuntuvim
algorithm
algorithm
master

搜索帮助