排序
来源:互联网 发布:阿里云服务器压力测试 编辑:程序博客网 时间:2024/05/01 16:40
#include <stdio.h>
void sort(int a[10]);
void main()
{
int a [10];
for(int i = 0; i<10; i++)
{
printf("enter a integer for a[%d]/n", i);
scanf("%d", &a[i]);
}
sort(a);
for( i = 0; i<10; i++)
{
printf("%d ", a[i]);
}
}
/************************************************************************/
/* 插入排序的时间复杂度为O(n^2),空间复杂度为O(1),是一种就地排序的算法
/************************************************************************/
void sort(int a[10])
{
for(int i = 1; i<10; i++)
{
//从当前元素的前一个元素开始比较
int j = i-1;
//保存当前元素,并滕出了其空间为数组元素的移动使用
int tem = a[i];
while(j>=0)
{
if (tem<a[j])
{
a[j+1] = a[j];
j--;
}
else
{
break;
}
}
//由于J在最后一次移动后仍然减了1,所以需要复位
a[j+1] = tem;
}
}
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- sqlserver2005使用convert转换日期[资料]
- 链表
- 影响Lucene索引速度原因以及提高索引速度技巧(收集)
- 如何在java中调用js方法
- 学习STL
- 排序
- C语言的static关键字的理解
- C++中const用法浅析
- 支付宝及时到帐接口使用详解
- nsis中 单句跳转的陷阱
- 深入分析C++中char * 和char []的区别
- Apache 安装过程中出错/usr/lib/libexpat.so: could not read symbols: File in wrong format的解决办法
- 2010年2月8日——明天就回家了!
- txt文件读写