zoukankan      html  css  js  c++  java
  • 希尔排序

    图示

       

    参考代码

    void shellSort(int A[], int lens)
    {
        if (A == NULL || lens <=0)
            return;
        for (int gap = lens/2; gap >0; gap /= 2)
        {
            for (int i = gap; i < lens; i+=1)
            {
                for (int j = i-gap; j >= 0 && A[j] > A[j+gap]; j-=gap)
                    swap(A[j], A[j+gap]);
            }
        }
    }

    测试

    #include <iostream>
    using namespace std;
    
    void shellSort(int A[], int lens)
    {
        if (A == NULL || lens <=0)
            return;
        for (int gap = lens/2; gap >0; gap /= 2)
        {
            for (int i = gap; i < lens; i+=1)
            {
                for (int j = i-gap; j >= 0 && A[j] > A[j+gap]; j-=gap)
                    swap(A[j], A[j+gap]);
            }
        }
    }
    
    void tranverse(int A[], int lens)
    {
        for (int i = 0; i < lens; ++i)
            cout << A[i] << " ";
        cout << endl;
    }
    
    int main()
    {
        int A[] = {5, 2, 9, 1, 3, 2, 2, 7};
        int lens = sizeof(A) / sizeof(*A);
        tranverse(A, lens);
        shellSort(A, lens);
        tranverse(A, lens);
    }
    View Code

    性能

    空间复杂度:O(1)

    时间复杂度好于O(n2)

    稳定性

    不稳定

  • 相关阅读:
    随笔
    我的舅舅
    代码规范
    SpringMVC_乱码问题
    SpringMVC_接受请求及数据回显
    Restful风格
    第六周总结
    SpringMVC_控制器
    SpringMVC_初次使用
    SpringMVC_简介
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/3971298.html
Copyright © 2011-2022 走看看