排序算法之插入排序

算法描述:

  1. 从第一个元素开始,该元素可以认为已经被排序
  2. 取出下一个元素,在已经排序的元素序列中从后向前扫描
  3. 如果该元素(已排序)大于新元素,将该元素移到下一位置
  4. 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
  5. 将新元素插入到该位置后
  6. 重复步骤2~5

度量:

数据结构     数组
最差时间复杂度     O(n^2)
最优时间复杂度     O(n)
平均时间复杂度     O(n^2)
最差空间复杂度     总共O(n) ,需要辅助空间O(1)
最佳算法     No


源码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

void print_ary( int * ary, unsigned int len )
{
    while( len-- > 0 ) {
        printf("%d ", *ary++ );    
    }
    putchar('\n');
}

void create_rondom_number(int **parray, unsigned int number)
{
    struct timeval tpstart;
    unsigned int i =0;

    if (NULL != *parray)
    {
        return;
    }

    *parray = (int *)malloc(sizeof(unsigned int) * number);
    (void)memset(*parray, 0, sizeof(unsigned int) * number);
    
    gettimeofday(&tpstart,NULL);

    //srandom(time(NULL));
    srandom(tpstart.tv_usec);

    for (i=0; i<number; ++i)
    {
        //(*parray)[i] = random()%number; // range is [0, number-1] 
        //(*parray)[i] = random()%(b-a) + a; //range is [a, b)
        ((*parray)[i] = 1 + (int) ((double)number * rand() / (RAND_MAX + 1.0))); // [1, number]
    }
}

void insertion_sort(int array[], unsigned int n)
{
    int  i,j;
    int temp;
    int first = 0; 
    int last = n - 1;

    for (i = first + 1; i <= last; i++)
    {
        temp = array[i];
        j=i-1;

        //printf("\n\ni=%d, j=%d temp=%d: ", i, j, temp);
        printf("\n\nstart from array[%d], temp=array[%d]=%d: ", j, i, temp);
        print_ary( array, n);

        /*与已排序的數逐一比較,大於temp時,該數向後移*/
        while((j>=first) && (array[j] > temp))  //当first=0,j循环到-1时,由于[[短路求值]],
                                                 //不会运算array[-1], 但j和first必须是int性,否则会有隐性的强制转换
        {
             array[j+1] = array[j];
             array[j] = 100000;
             printf("--array[%d] move to array[%d]: ", j, j+1);
             print_ary( array, n);
             j--;
        }

        array[j+1] = temp;      //被排序数放到正确的位置
        printf("**insert temp=%d to array[%d]: ", temp, j+1);
        print_ary( array, n);
    } 

}

int main( int argc, char ** argv )
{

    int *pary = NULL;

    if (argc !=2)
    {
        printf("\nUsage: %s Number\n", argv[0]); 
        return -1;
    }

    create_rondom_number(&pary, (unsigned int)atoi(argv[1]));    

    printf("\n\nrandom number: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );

    printf("Start sortting: \n");
    insertion_sort( pary, (unsigned int)atoi(argv[1]));

    printf("\n\nafter sorted: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );
    
    return 0;
}


运行结果:

[root@localhost sort]# ./insert 10


random number: 1 6 4 6 9 9 2 1 3 5
Start sortting:


start from array[0], temp=array[1]=6: 1 6 4 6 9 9 2 1 3 5
**insert temp=6 to array[1]: 1 6 4 6 9 9 2 1 3 5


start from array[1], temp=array[2]=4: 1 6 4 6 9 9 2 1 3 5
--array[1] move to array[2]: 1 100000 6 6 9 9 2 1 3 5
**insert temp=4 to array[1]: 1 4 6 6 9 9 2 1 3 5


start from array[2], temp=array[3]=6: 1 4 6 6 9 9 2 1 3 5
**insert temp=6 to array[3]: 1 4 6 6 9 9 2 1 3 5


start from array[3], temp=array[4]=9: 1 4 6 6 9 9 2 1 3 5
**insert temp=9 to array[4]: 1 4 6 6 9 9 2 1 3 5


start from array[4], temp=array[5]=9: 1 4 6 6 9 9 2 1 3 5
**insert temp=9 to array[5]: 1 4 6 6 9 9 2 1 3 5


start from array[5], temp=array[6]=2: 1 4 6 6 9 9 2 1 3 5
--array[5] move to array[6]: 1 4 6 6 9 100000 9 1 3 5
--array[4] move to array[5]: 1 4 6 6 100000 9 9 1 3 5
--array[3] move to array[4]: 1 4 6 100000 6 9 9 1 3 5
--array[2] move to array[3]: 1 4 100000 6 6 9 9 1 3 5
--array[1] move to array[2]: 1 100000 4 6 6 9 9 1 3 5
**insert temp=2 to array[1]: 1 2 4 6 6 9 9 1 3 5


start from array[6], temp=array[7]=1: 1 2 4 6 6 9 9 1 3 5
--array[6] move to array[7]: 1 2 4 6 6 9 100000 9 3 5
--array[5] move to array[6]: 1 2 4 6 6 100000 9 9 3 5
--array[4] move to array[5]: 1 2 4 6 100000 6 9 9 3 5
--array[3] move to array[4]: 1 2 4 100000 6 6 9 9 3 5
--array[2] move to array[3]: 1 2 100000 4 6 6 9 9 3 5
--array[1] move to array[2]: 1 100000 2 4 6 6 9 9 3 5
**insert temp=1 to array[1]: 1 1 2 4 6 6 9 9 3 5


start from array[7], temp=array[8]=3: 1 1 2 4 6 6 9 9 3 5
--array[7] move to array[8]: 1 1 2 4 6 6 9 100000 9 5
--array[6] move to array[7]: 1 1 2 4 6 6 100000 9 9 5
--array[5] move to array[6]: 1 1 2 4 6 100000 6 9 9 5
--array[4] move to array[5]: 1 1 2 4 100000 6 6 9 9 5
--array[3] move to array[4]: 1 1 2 100000 4 6 6 9 9 5
**insert temp=3 to array[3]: 1 1 2 3 4 6 6 9 9 5


start from array[8], temp=array[9]=5: 1 1 2 3 4 6 6 9 9 5
--array[8] move to array[9]: 1 1 2 3 4 6 6 9 100000 9
--array[7] move to array[8]: 1 1 2 3 4 6 6 100000 9 9
--array[6] move to array[7]: 1 1 2 3 4 6 100000 6 9 9
--array[5] move to array[6]: 1 1 2 3 4 100000 6 6 9 9
**insert temp=5 to array[5]: 1 1 2 3 4 5 6 6 9 9


after sorted: 1 1 2 3 4 5 6 6 9 9
[root@localhost sort]#

参考:

http://zh.wikipedia.org/wiki/%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F

http://baike.baidu.com/view/396887.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值