Internal Sorting: Shellsort: Sorting by Insertion

Shellsort:希尔排序


Animation

shellsort
Shellsort with gaps 23, 10, 4, 1 in action.


这里写图片描述
The step-by-step process of replacing pairs of items during the shell sorting algorithm.


Complexity

ClassSorting algorithm
Data structureArray
Worst case performance O(n2)
Best case performance O(nlog2n)
Average case performancedepends on gap sequence
Worst case space complexity О(n) total, O(1) auxiliary

Algorithm D

Algorithm D (Shellsort).Records R1,...,RN are rearranged in place; after
sorting is complete, their keys will be in order, K1<=...<=KN .Anauxiliary
sequence of increments ht1,ht2,...,h0 is used to control the sorting process,
where h0=1 ;proper choice of these increments can significantly decrease the
sorting time. This algorithm reduces to Algorithm S when t=1 .
D1. [Loop on s .] Performstep D2 for s=t1,t2,...,0; then terminate the
algorithm.
D2. [Loop on j .] Set hhs, and perform steps D3 through D6 for h
(We will use a straight insertion method to sort elements that are h positions
apart, so that Ki<=Ki+h for 1<=i<=Nh . Steps D3 through D6 are
essentially the same as steps S2 through S5, respectively, in Algorithm S.)
D3. [Set up i,K,R .]Set ijh,KKj,RRj .
D4. [Compare K:Ki .] If K>=Ki , go to step D6.
D5. [Move Ri , decrease i .] Set Ri+hRi, then iih . If i>0 , go back to
step D4.
D6. [ R into Ri+h.] Set Ri+hR . |


Data table

Shellsort:Sorting by Insertion:Internal Sorting


Java program

In this program, R1,…,RN weresimplified to K1,…,KN.

/**
 * Created with IntelliJ IDEA.
 * User: 1O1O
 * Date: 11/23/13
 * Time: 10:01 PM
 * :)~
 * Shellsort:Sorting by Insertion:Internal Sorting
 */
public class Main {

    public static void main(String[] args) {
        int N = 16;
        int[] K = new int[17];

        /*Prepare the data*/
        K[1] = 503;
        K[2] = 87;
        K[3] = 512;
        K[4] = 61;
        K[5] = 908;
        K[6] = 170;
        K[7] = 897;
        K[8] = 275;
        K[9] = 653;
        K[10] = 426;
        K[11] = 154;
        K[12] = 509;
        K[13] = 612;
        K[14] = 677;
        K[15] = 765;
        K[16] = 703;

        /*Output unsorted Ks*/
        System.out.println("Unsorted Ks:");
        for(int i=1; i<=N; i++){
            System.out.println(i+":"+K[i]);
        }
        System.out.println();

        /*Kernel of the Algorithm!*/
        for(int h=N/2; h>=1; h/=2){
            for(int j=h+1; j<=N; j+=h){
                int Key = K[j];
                int i = j-h;
                while (i > 0){
                    if(Key >= K[i]){
                        K[i+h] = Key;
                        break;
                    }else {
                        K[i+h] = K[i];
                        i-=h;
                    }
                }
                if(i <= 0){
                    K[i+h] = Key;
                }
            }
        }

        /*Output sorted Ks*/
        System.out.println("Sorted Ks:");
        for(int i=1; i<=N; i++){
            System.out.println(i+":"+K[i]);
        }
    }
}

Outputs

UnsortedKs:
1:503
2:87
3:512
4:61
5:908
6:170
7:897
8:275
9:653
10:426
11:154
12:509
13:612
14:677
15:765
16:703

SortedKs:
1:61
2:87
3:154
4:170
5:275
6:426
7:503
8:509
9:512
10:612
11:653
12:677
13:703
14:765
15:897
16:908

Reference

<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH
https://en.wikipedia.org/wiki/Shellsort

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你要在C++中实现插入排序算法时,可以将其拆分为多个文件以提高代码的可读性和可维护性。下面是一个可能的实现示例: 首先,创建一个名为`insertion_sort.h`的头文件,用于声明插入排序函数: ```cpp #ifndef INSERTION_SORT_H #define INSERTION_SORT_H namespace Sorting { void insertionSort(int arr[], int size); } #endif ``` 然后,在`insertion_sort.cpp`文件中实现插入排序算法: ```cpp #include "insertion_sort.h" namespace Sorting { void insertionSort(int arr[], int size) { for (int i = 1; i < size; ++i) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } } ``` 接下来,你可以创建一个名为`main.cpp`的文件来测试插入排序算法: ```cpp #include <iostream> #include "insertion_sort.h" int main() { int arr[] = {5, 2, 8, 1, 3}; int size = sizeof(arr) / sizeof(arr[0]); std::cout << "Before sorting: "; for (int i = 0; i < size; ++i) { std::cout << arr[i] << " "; } std::cout << std::endl; Sorting::insertionSort(arr, size); std::cout << "After sorting: "; for (int i = 0; i < size; ++i) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; } ``` 现在,你可以通过`g++`编译器将这些文件编译在一起: ```shell g++ main.cpp insertion_sort.cpp -o insertion_sort ``` 最后,运行生成的可执行文件: ```shell ./insertion_sort ``` 这将输出排序前后的数组。这种方式能够使代码更加模块化,易于理解和维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值