ShellSort

Posted purzel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ShellSort相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>

using namespace std;
#define MAXSIZE 200000
typedef int KeyType;
typedef struct {
    KeyType key;
}RedType;
typedef struct {
    RedType r[MAXSIZE + 1];
    int length;
}SqList;
int Random(int start, int end){
    int dis = end - start;
    return rand() % dis + start;
}
void ShellInsert(SqList &L,int dk) {
    int i, j;
    for (i = dk + 1; i <= L.length; ++i) 
        if(L.r[i].key < L.r[i - dk].key) {
            L.r[0] = L.r[i];
            for (j = i - dk; j > 0 && L.r[0].key < L.r[j].key; j -= dk) 
                L.r[j + dk] = L.r[j];
            L.r[j + dk] = L.r[0];
        }
}
void ShellSort (SqList &L, int dt[], int t) {
    double start_time, finish_time, cord_time;
    start_time = clock();
    int k;
    for (k = 0; k < t; ++k) {
        ShellInsert(L, dt[k]);
    }
    finish_time = clock();
    cord_time = (double)(finish_time - start_time) ;
    printf("ShellSort time=%f ms
", cord_time);
    // int i;
    // for (i = 0; i < 3; ++i)
    //     printf("%d
",dt[i]);
}
// void Zengliang(SqList &L, int dt[]) {
//     for (int i = 1; i <=L.length; ++i) {
//         dt[i-1] = pow(2, i) - 1;
//     }
// }
void InPut(SqList &L) {
    int i;
    srand((unsigned)time(NULL));
    cin >> L.length;
    for (i = 1; i <= L.length; ++i) {
        // cin >> L.r[i].key;
        L.r[i].key = Random(1, 1000000);
    }
}
void OutPut(SqList &L) {
    int i;
    for (i = 1; i <= L.length; ++i) {
        cout << L.r[i].key << " ";
    }
}
// void test (SqList &L, int dt[]) {
//     int i;
//     for (i = 0; i < L.length; ++i)
//         printf("%d
",dt[i]);
// }
int main() {
    int dt[3] = {5, 3, 1};
    SqList L;
    // L.r = new RedType [MAXSIZE+1];
    InPut(L);
    // Zengliang(L, dt);
    // test(L, dt);
    ShellSort(L, dt, 3);
    OutPut(L);
    return 0;
}

 

以上是关于ShellSort的主要内容,如果未能解决你的问题,请参考以下文章

ShellSort 希尔排序

ShellSort uva

ShellSort

ShellSort

shellsort命令

ShellSort