希尔排序算法

1、算法说明

先取一个小于n的整数d1作为第一个 增量,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行 直接插入排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量
   
=1(
   
<
   
…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。
该方法实质上是一种分组插入方法
比较相隔较远距离(称为 增量)的数,使得数移动时能跨过多个元素,则进行一次比 [2]   较就可能消除多个元素交换。D.L.shell于1959年在以他名字命名的 排序算法中实现了这一思想。算法先将要排序的一组数按某个 增量d分成若干组,每组中记录的下标相差d.对每组中全部元素进行排序,然后再用一个较小的增量对它进行,在每组中再进行排序。当 增量减到1时,整个要排序的数被分成一组,排序完成。
一般的初次取序列的一半为 增量,以后每次减半,直到增量为1。

2、无调试信息源代码

#include <iostream>

using namespace std;

void sort_shell(int [], int);
void swap(int [], int []);
void output(int [], int);

int main(int argc, char *argv[])
{
    int num = 0;
    cin >> num;
    if(num <= 0) return 0;

    int array[num];
    for(int i = 0; i < num; i++) cin >> array[i];
    sort_shell(array, num);
    output(array, num);

    return 0;
}


void sort_shell(int a[], int n)
{
    int h, i, j, t;
    for(h = n / 2; h >= 1; h /= 2) {
        for(i = h; i < n; i++) {
            t = a[i];
            for(j = i - h; j >= 0 && t < a[j]; j -= h) {
                a[j + h] = a[j];
            }
            a[j + h] = t;
        }
    }
}

void output(int a[], int n)
{
    if(!a || n <= 0) return;
    for(int i = 0; i < n; i++) cout << a[i] << " ";
    cout << endl;
}

3、有调试信息源代码

#include <iostream>

using namespace std;

#define debug 1

void sort_shell(int [], int);
void swap(int [], int []);
void output(int [], int);

int main(int argc, char *argv[])
{
    int num = 0;
    if(debug) cout << "Please input an integer." << endl;
    cin >> num;
    if(num <= 0) return 0;

    int array[num];
    if(debug) cout << "Please input " << num << " integers." << endl;
    for(int i = 0; i < num; i++) cin >> array[i];
    if(debug) {
        cout << "Before sorting, the numbers are as following:" << endl;
        output(array, num);
    }

    sort_shell(array, num);
    if(debug) cout << "After sorting, the numbers are as following:" << endl;
    output(array, num);

    return 0;
}

void sort_shell(int a[], int n)
{
    int h, i, j, t;
    for(h = n / 2; h >= 1; h /= 2) {
        for(i = h; i < n; i++) {
            t = a[i];
            for(j = i - h; j >= 0 && t < a[j]; j -= h) {
                a[j + h] = a[j];
                if(debug) {
                    cout << "S1: " << j << ", " << j + h << ":  ";
                    cout << "t = " << t << ":  ";
                    output(a, n);
                }
            }
            a[j + h] = t;
            if(debug) {
                cout << "S2: " << j + h << ", " << i <<  ":  ";
                cout << "t = " << t << ":  ";
                output(a, n);
            }
        }
    }
}

void output(int a[], int n)
{
    if(!a || n <= 0) return;
    for(int i = 0; i < n; i++) cout << a[i] << " ";
    cout << endl;
}

4、调试说明

需要查看具体排序过程:
#define debug 1  
不需要查看具体排序过程:

#define debug 0

5、运行结果

输入:

10
49 38 65 97 76 13 27 49 55 4

输出:

Please input an integer.
Please input 10 integers.
Before sorting, the numbers are as following:
49 38 65 97 76 13 27 49 55 4 
S1: 0, 5:  t = 13:  49 38 65 97 76 49 27 49 55 4 
S2: 0, 5:  t = 13:  13 38 65 97 76 49 27 49 55 4 
S1: 1, 6:  t = 27:  13 38 65 97 76 49 38 49 55 4 
S2: 1, 6:  t = 27:  13 27 65 97 76 49 38 49 55 4 
S1: 2, 7:  t = 49:  13 27 65 97 76 49 38 65 55 4 
S2: 2, 7:  t = 49:  13 27 49 97 76 49 38 65 55 4 
S1: 3, 8:  t = 55:  13 27 49 97 76 49 38 65 97 4 
S2: 3, 8:  t = 55:  13 27 49 55 76 49 38 65 97 4 
S1: 4, 9:  t = 4:  13 27 49 55 76 49 38 65 97 76 
S2: 4, 9:  t = 4:  13 27 49 55 4 49 38 65 97 76 
S2: 2, 2:  t = 49:  13 27 49 55 4 49 38 65 97 76 
S2: 3, 3:  t = 55:  13 27 49 55 4 49 38 65 97 76 
S1: 2, 4:  t = 4:  13 27 49 55 49 49 38 65 97 76 
S1: 0, 2:  t = 4:  13 27 13 55 49 49 38 65 97 76 
S2: 0, 4:  t = 4:  4 27 13 55 49 49 38 65 97 76 
S1: 3, 5:  t = 49:  4 27 13 55 49 55 38 65 97 76 
S2: 3, 5:  t = 49:  4 27 13 49 49 55 38 65 97 76 
S1: 4, 6:  t = 38:  4 27 13 49 49 55 49 65 97 76 
S2: 4, 6:  t = 38:  4 27 13 49 38 55 49 65 97 76 
S2: 7, 7:  t = 65:  4 27 13 49 38 55 49 65 97 76 
S2: 8, 8:  t = 97:  4 27 13 49 38 55 49 65 97 76 
S2: 9, 9:  t = 76:  4 27 13 49 38 55 49 65 97 76 
S2: 1, 1:  t = 27:  4 27 13 49 38 55 49 65 97 76 
S1: 1, 2:  t = 13:  4 27 27 49 38 55 49 65 97 76 
S2: 1, 2:  t = 13:  4 13 27 49 38 55 49 65 97 76 
S2: 3, 3:  t = 49:  4 13 27 49 38 55 49 65 97 76 
S1: 3, 4:  t = 38:  4 13 27 49 49 55 49 65 97 76 
S2: 3, 4:  t = 38:  4 13 27 38 49 55 49 65 97 76 
S2: 5, 5:  t = 55:  4 13 27 38 49 55 49 65 97 76 
S1: 5, 6:  t = 49:  4 13 27 38 49 55 55 65 97 76 
S2: 5, 6:  t = 49:  4 13 27 38 49 49 55 65 97 76 
S2: 7, 7:  t = 65:  4 13 27 38 49 49 55 65 97 76 
S2: 8, 8:  t = 97:  4 13 27 38 49 49 55 65 97 76 
S1: 8, 9:  t = 76:  4 13 27 38 49 49 55 65 97 97 
S2: 8, 9:  t = 76:  4 13 27 38 49 49 55 65 76 97 
After sorting, the numbers are as following:
4 13 27 38 49 49 55 65 76 97 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值