zoukankan      html  css  js  c++  java
  • 深入浅出理解排序算法之-插入排序

    #include <iostream>


    /* 插入排序


     基本思想:将记录插入到已排序好的有序表中

     特点:一种稳定的排序方法,时间复杂度O(n^2)

     */

    void InsertSort(int array[],int len){

        int i,j;

        int temp;

        

        for (i =1; i < len; i++) {        // i1 ~ <len(i++)

            temp = array[i];

            for (j = i -1; j >=0; j--) { // ji-1 ~ >=0(j--)

                if (temp < array[j]) {     //假设待插入元素<前面序列,则后移元素

                    array[j+1] = array[j];

                }else{

                    break;

                }

            }

            array[j+1] = temp;             //空位填上待插入元素就可以

        }

    }


    int main(int argc,constchar * argv[])

    {

        int i =0;

        int a[] = {5,4,9,8,7,6,0,1,3,2};

        int len =sizeof(a)/sizeof(a[0]);

        

        // 插入排序

        InsertSort(a,len);

        

        for (i =0; i < len; i++) {

            printf("%d ",a[i]);

        }

        printf(" ");

        

        return0;

    }


  • 相关阅读:
    前端HTMLCSS
    jedis 连接池的使用
    win8+安装net3.5步骤与常见错误.
    并行线程的生命周期
    OneNote截图快捷键冲突解决方案
    C#中lsitView如何搜索某个子项
    redis哨兵与集群
    git笔记
    微软官方Hololens开发课程介绍
    Markdown使用入门简介
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4216901.html
Copyright © 2011-2022 走看看