zoukankan      html  css  js  c++  java
  • 1209.2——直接插入排序算法

    #include <stdio.h>

    //直接插入排序 5,4,6,2,1

    //4 5 6

    //6

    void insertSort(int array[], int elementNum){

        int referenceNum = 0;//保存即将插入的那个值

        

        for (int i = 1; i < elementNum; i++) {

            referenceNum = array[i];

            

            int j = i - 1;

            //通过一个循环查找这个值应该插在哪个位置

            for (; j >= 0; j--) {

                if (array[j] > referenceNum) {

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

                } else{

                    //说明找到要插入的位置了

                    break;

                }

            }

            

            //1.根本就没有移动

            //2.已经移动过了

            if (j+1 != i) {

                //动过了

                array[j+1] = referenceNum;

            }

        }

    }

    int main(int argc, const char * argv[]) {

        int array[] = {5,4,6,2,1};

        

        insertSort(array, 5);

        

        for (int i = 0; i < 5; i++) {

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

        }

        printf(" ");

        return 0;

    }

  • 相关阅读:
    python打包生成exe文件
    Django建表
    Pycharm 激活码2017最新
    源码包安装Mysql
    Python 的PIL,可以解决ImportError The _imagingft C module is not installed
    远方的塔--Pylons
    Flask 框架入门
    缓动公式
    js源生惯性滚动与回弹(备用)
    javascript实现继承的12种套路
  • 原文地址:https://www.cnblogs.com/damonWq/p/5033034.html
Copyright © 2011-2022 走看看