zoukankan      html  css  js  c++  java
  • 算法笔记 --- Shell Sort

    #include <iostream>
    
    using namespace std;
    class ShellSort {
    public:
        int* shellSort(int* A, int n) {
            // write code here
            for(int gap = n / 2; gap > 0; gap /= 2){
                for(int index_beg = 0; index_beg < gap; index_beg++){
                    insertionSort(A, n, gap, index_beg);
                }
            }
            return A;
        }
        void insertionSort(int* A, int n, int gap, int index_beg) {
            // write code here
            int tmp;
            int index_insert;
            for(int index = index_beg+gap; index < n; index += gap){
                index_insert = index;
                while(A[index_insert] < A[index_insert - gap] && index_insert >= gap){
                    tmp = A[index_insert];
                    A[index_insert] = A[index_insert - gap];
                    A[index_insert - gap] = tmp;
                    index_insert-=gap;
                }
            }
        }
    };
    int main()
    {
        int a[6] = {1, 5, 7, 4, 2, 9};
        int* res;
        ShellSort sorter;
        res = sorter.shellSort(a, 6);
        cout<<"after sorting:"<<endl;
        for(int i = 0; i < 6; i++){
            cout<<a[i]<<" ";
        }
        cout<<endl;
       
        return 0;
    }
  • 相关阅读:
    一种复杂的情感--“外戚”
    追~
    神受的孩子
    不好的习惯
    思杨改编的朝代歌
    siyang入厕
    小思趣事
    今天周三啦~~时光啊
    Python(6)——装饰器
    Python(5)——关于描述符的三种操作方法
  • 原文地址:https://www.cnblogs.com/zhongzhiqiang/p/5791094.html
Copyright © 2011-2022 走看看