灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2679回复:0

C语言排序系列之插入排序(1)

楼主#
更多 发布于:2012-09-06 12:32

插入排序是最简单最粗暴的排序方式,其基本思想是:对于已经有序的前i-1个数字,将第i个数字插入至合适位置
    时间复杂度为:O(n^2)
  

/*插入排序
  时间复杂度:O(n^2)
*/
#include<stdio.h>
void Swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
void InsertSort(int data[],int length)
{
    int i = 0;
    int j = 0;
    for(i = 1;i < length;++i)
    {
        for(j = i;j > 0;--j)
        {
            if(data[j] < data[j - 1])
            {
                Swap(;data[j], ;data[j - 1]);
            }
            else
            {
                break;
            }
        }
    }
}
int main()
{
    int data[8] = {4,7,2,6,5,9,3,8};
    int i = 0;
    InsertSort(data,8);
    for(i = 0;i < 8;i++)
    {
        printf("%d ",data);
    }
    printf("\n");
    getchar();
    return 0;
}


摘自 泡泡腾


喜欢0 评分0
游客

返回顶部