Hill sorting by Sorting Algorithm

Source: Internet
Author: User
Hill sorting by Sorting Algorithm

Shell sort, also known as incremental sorting, is an input insertion sorting algorithm that improves the direct sorting algorithm. This article introduces the hill sorting algorithm.

For the insert sorting algorithm, if the original data is ordered, the data does not need to be moved, and the efficiency of the insert sorting algorithm is mainly consumed in the movement of data. Therefore, we can see that if the data itself is ordered or basically ordered, the efficiency will be improved.

The basic idea of hill sorting is to divide the sequence to be sorted into several smaller sub-sequences, and sort the insertion of sub-sequences. Then, the insertion sorting can make the original sequence a basic order. In this way, the insertion sorting of small sequences and insertion sorting of basic ordered series can improve the efficiency of the insertion sorting algorithm.

In the hill sorting, the first solution is the selection of subsequences. The sub-sequence is not a simple segmentation, but a sequence is composed of incremental data. The general choice principle is: The next increment is generally used as the Division increment of the current sequence. The length of the sequence selected for the first time is generally incremental.

Assume that the length of the array is 10, and the array elements are: 25, 19, 6, 58, and.

The process of the entire hill sorting algorithm is as follows:

Is the raw data and the incremental D selected for the first time = 5. The result of this sorting is as follows:

This is the result of the first sorting. the incremental value of this selection is d = 2. The result of this sorting is as follows:

When d = 1 is the last sorting, This sorting is equivalent to a loop of Bubble sorting. The final result is as follows:

In actual use, there are certainly not only 10 Data with sorting, but the above is the idea of hill sorting. In fact, Hill sorting is only an optimization of insert sorting.

Shows the entire process of hill sorting. Assume that the data structure with sorting is an integer array. The length of the array a [n] is N.

The code for C ++ to implement Hill sorting is as follows:

// Shellsort. cpp: defines the entry point for the console application.

//

# Include "stdafx. H"

# Include <iostream>

Using namespace STD;

Void display (INT array [], int length );

Void shellsort (INT array [], int length );

Int main (INT argc, char * argv [])

{

Int arr [10] = };

Display (ARR, 10 );

Shellsort (ARR, 10 );

Display (ARR, 10 );

System ("pause ");

Return 0;

}

// Output Array

Void display (INT array [], int length)

{

For (INT I = 0; I <length; I ++)

{

Cout <array [I] <",";

}

Cout <Endl;

}

// Implement the hill Sorting Algorithm

Void shellsort (INT array [], int length)

{

Int d = length/2; // sets the increment of the hill sorting.

Int I;

Int J;

Int temp;

While (D> = 1)

{

For (I = D; I <length; I ++)

{

Temp = array [I];

J = I-d;

While (j> = 0 & array [J]> temp)

{

Array [J + D] = array [J];

J = J-D;

}

Array [J + D] = temp;

}

Display (array, 10 );

D = D/2; // reduce the Increment

}

}

/*

Hill sorting is an improvement on insert sorting. The idea and code of this algorithm are both complex. The above code is successfully run in VC 6.0.

*/

From: http://hi.baidu.com/gsgaoshuang/item/17a8ed3c24d9b1ba134b14c2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.