博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【排序】InsertSort
阅读量:7187 次
发布时间:2019-06-29

本文共 1010 字,大约阅读时间需要 3 分钟。

Why does it call insert sort?

It's name contains the method it uses.

It's basic theory was so simple:  Insert a element into a sorted list. 

But how the sorted list basically initialize ---- build a list with only one element.

Here comes the code:

#include <stdio.h>
#include 
"
type.h
"
void InsertSort(SqList *L)
{
    
int i,j,com;
    
for( i= 
1; i< L->length; i++)
    {
        
if(L->data[i]<L->data[i-
1])
        {
            com = L->data[i];
            
for( j= i; com < L->data[j-
1] ; j--)
            {
                L->data[j]=L->data[j-
1];
            }
            L->data[j]= com;
        }
    }
}
void printContent(SqList *L)
{
    
for(
int i = 
0; i< L->length; i++)
    {
        printf(
"
%d \t
",L->data[i] );
    }
}
int main(
void)
{
    SqList l ;
    
    l.data[
0] = 
9;
    l.data[
1] = 
1;
    l.data[
2] = 
5;
    l.data[
3] = 
8;
    l.data[
4] = 
3;
    l.data[
5] = 
7;
    l.data[
6] = 
4;
    l.data[
7] = 
6;
    l.data[
8] = 
2;
    
    l.length = 
9;
    printContent(&l);
    printf(
"
\n
");
    InsertSort(&l);
    printContent(&l);
    printf(
"
\n
");
    
return 
0;
}

 

There are already about Insert Sort.

It's complexity : worse : O(n2), but a litte better than select sort and bubble sort.

转载地址:http://jcykm.baihongyu.com/

你可能感兴趣的文章
用javascript判断IE版本号简单实用且向后兼容
查看>>
Java_Ant详解
查看>>
AWS考证方向:五、使用key密钥连接实例
查看>>
Ganglia 安装与配置详解
查看>>
易宝典文章——怎样管理Exchange Server 2013共享邮箱
查看>>
UILabel根据字数多少自动实现适应高度
查看>>
Warning: lio_listio returned EAGAIN Performance degradation may be seen
查看>>
我的友情链接
查看>>
iOS开发必备HUD(透明指示层)
查看>>
mysql删除大表的部分数据
查看>>
Linux内核升级
查看>>
java枚举常用配置
查看>>
本人的性格弱点
查看>>
C#实现一维小波变换
查看>>
PHP内核定义变量的方式
查看>>
hadoop零散笔记
查看>>
uboot移植——uboot启动回顾
查看>>
分段机制和分页机制
查看>>
三天学会HTML5 之第一天
查看>>
学习《计算机网络》思路总结
查看>>