• 算法导论第二章算法入门2.1 插入排序


    O)GHKRR4208[Z98SOA`M)FJ

    OO@QSG1C]A2EP{H2WMXAC5W

    EV{X(F7V)2%`YB4FIVIFI29

    }V(K`}BT}N51XQ}`3WSR$GP

    B0N`B)C)N9DMFQ@1{2N3H7T

    插入排序

    // insertion_sort.h not with template
    #include <iostream>
    #include <stdint.h>
    // INSERTION-SORT(A)
    // for j = 2 to A.length
    //     key = A[j]
    //     // Insert A[j] into sorted sequence A[1..j - 1].
    //     i = j - 1
    //     while i > 0 and a[i] > key
    //         a[i + 1] = a[i]
    //         i = i - 1
    //     a[i + 1] = key
    
    // allow the same key
    
    void insertion_sort(uint64_t* A, uint64_t const n)
    {   
        uint64_t key;
        int64_t i;
        // j = 2 to A.length(j 1 to n - 1)
        for (uint64_t j = 1; j < n; j++) // j begin with A[1]
        {
            key = A[j];
            i = j - 1;                   // i begin with A[0]
            while (i >= 0 && A[i] > key)
            {
                A[i + 1] = A[i];
                i--;
            }
            A[i + 1] = key;
        }
    }
    
    // insertion_sort.cpp
    #include "insertion_sort.h"
    
    #ifdef __linux
    #include <stdio.h>
    #endif
    
    int main()
    {
        uint64_t array[6] = { 5, 2, 4, 6, 1, 3 };
        for (uint64_t i = 0; i < sizeof(array) / sizeof(uint64_t); i++)
        {
            std::cout << array[i] << " ";
        }
        std::cout << std::endl;
        insertion_sort(array, sizeof(array) / sizeof(uint64_t));
        for (uint64_t i = 0; i < sizeof(array) / sizeof(uint64_t); i++)
        {
            std::cout << array[i] << " ";
        }
        std::cout << std::endl;
        getchar();
        return 0;
    }

    插入排序模版

    // 插入排序 insertion_sort_t.h
    
    #include <iostream>
    #include <string>
    
    // INSERTION-SORT(A)
    // for j = 2 to A.length
    //     key = A[j]
    //     // Insert A[j] into sorted sequence A[1..j - 1].
    //     i = j - 1
    //     while i > 0 and a[i] > key
    //         a[i + 1] = a[i]
    //         i = i - 1
    //     a[i + 1] = key
    
    template <class Type> void insert_sort_t(Type * const a, int const & n)
    {
        Type key;
        // j赋值为1因为是从第二个元素开始插入排序
        // j<n因为n代表着待排序的数组元素的个数,n-1为最后一个元素
        for (int j = 1; j < n; j++)
        {
            key = a[j];    // 等待插入的元素为a[j]
            int i = j - 1; // a[0...j-1]为已经有序的部分,a[j+1...n-1]为还没有排序的部分
            // 我们首先要比较的是a[j]与a[j-1]
            while ((i >= 0) && (a[i] > key))
            {
                a[i + 1] = a[i]; // 所有比a[j]大的元素后移一位
                i--;
            }
            a[i + 1] = key;      // 将a[j]放到正确的位置上去
        }
    }
    
    
    // insertion_sort_t.cpp
    #include "insertion_sort_t.h"
    
    #ifdef __linux
    #include <stdio.h>
    #endif
    
    int main()
    {
        int a[6] = { 5, 2, 4, 6, 1, 3 };
        insert_sort_t(a, 6);
        for (int i = 0; i < 6; i++)
        {
            std::cout << a[i] << " ";
        }
        std::cout << std::endl;
        std::string b[4] = { "hello", "China", "haha",  "I Love China"};
        insert_sort_t(b, 4);
        for (int i = 0; i < 4; i++)
        {
            std::cout << b[i] << " ";
        }
        std::cout << std::endl;
        getchar();
        return 0;
    }

    71eb2af7jw1dt06paf0yvj (2)71eb2af7jw1dt06paf0yvj71eb2af7jw1du2bczbdlvj71eb2af7jw1du2bl9h3fzj71eb2af7jw1du2g4kek5sj71eb2af7jw1e7glloascwj20k00zktel

    71eb2af7jw1e8arf4y16oj20a00hswex

    71eb2af7jw1e8m51y1xfbj20f00qo74v71eb2af7jw1e8rlwrq980j20p018ggob71eb2af7jw1e8s56f8ko4j20p018g40r71eb2af7jw1e94d375a3rj20f00qot97

    71eb2af7jw1e94d998q6sj20f00qo0t6

    71eb2af7jw1e862krdnncj20dc0hsjs6

    71eb2af7jw8e9h3e9pjmsj20f00qoq3y

    71eb2af7jw8e9h3erqsf3j20f00qoq3y

    
    
  • 相关阅读:
    ROS_Kinetic_24 使用catkin_create_qt_pkg快速创建qt-ros功能包
    ROS_Kinetic_23 ROS流行版本和相关书籍汇总
    Android 5.1 添加硬件抽象层(HAL)和JNI接口总结
    Android 5.1.1 源码目录结构
    数据化决策的魅力
    [Android L]SEAndroid开放设备文件结点权限(读或写)方法(涵盖常用操作:sys/xxx、proc/xxx、SystemProperties)热门干货
    Android实现系统ROOT, 并能赋予app root权限
    ROS_Kinetic_22 使用ROS的qt插件即ros_qtc_plugin实现Hi ROS!!!!
    android 关机充电流程
    linux qcom LCD framwork
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4270995.html
Copyright © 2020-2023  润新知