希尔排序

来源:互联网 发布:大数据时代知乎 编辑:程序博客网 时间:2024/04/28 12:06

希尔排序思想逻辑

这里写图片描述


希尔排序代码

void InsertShellSort(int32_t numList[], int32_t len, int32_t dk) {    for (auto i = dk; i < len; i++) {        auto preIndex = i - dk;        if (numList[i] < numList[preIndex]) {            auto tmp = numList[i];              while (tmp < numList[preIndex]) {                numList[preIndex + dk] = numList[preIndex];                preIndex -= dk;            }            numList[preIndex + dk] = tmp;        }    }}//希尔排序void ShellSort(int32_t numList[], int32_t len) {    auto dk = len / 2;    while (dk >= 1) {        InsertShellSort(numList, len, dk);        dk /= 2;    }}void main() {    int32_t numList[] = {70,30,40,10,80,20,90,100,75,60,45 };    int32_t count = sizeof(numList) / sizeof(numList[0]);    ShellSort(numList, count);    for (auto num : numList) {        std::cout << num << " ";    }    std::cout << std::endl;}

返回排序算法分析总结

0 0
原创粉丝点击