C ++ version of hill sorting

Source: Internet
Author: User

Put it bluntly and directly add the code

. H won't post any comments. Some of them come from a blog post, which is very helpful for understanding. So let's take it.

# Include "arraysh. H"
# Include <iostream>
Using namespace STD;

Arraysh: arraysh (INT size ){
Data = new long [size];
Items = 0;
}

Arraysh ::~ Arraysh (){
Delete data;
}

Void arraysh: insert (long d ){
Data [items ++] = D;
// Items ++;
Cout <"insert num is:" <D <Endl;

}

Void arraysh: Display (){

// Int Index = 0;
For (INT I = 0; I <items; I ++ ){
Cout <"data is:" <data [I] <Endl;
}

}

Long arraysh: Remove (INT index ){

Long L = 0;
If (index <items ){
L = data [Index];
Data [Index] = NULL;
Items --;
}

Return L;

}

Void arraysh: shellsort (){

Int H = 1;

While (H <= items/3) // calculates the maximum interval H.
H = 3 * H + 1;

While (h> 0) // determine whether to continue to split data columns by narrowing the interval H
{

/* Why does out start with H? The first subsequence you split should be such a sequence, 0, h, 2 h, 3 h ,...
The while loop of insert sorting starts from 1, because the first number is always ordered and there is no need to compare. This requires understanding of the insert sorting algorithm, so the comparison is from the second data line, is the beginning of the H subscript of the array.
Why is out determined? <Len?
Control the array subscript. The following example will explain:

The following is an example.
Assume that there is an array of 10 data items, and the array subscript ranges from 0 ~ 9 indicates
This is the subsequence when H = 4. The following mark indicates
(0 4 8) (1 5 9) (2 6) (3 7)
I understood this for the first time. I really sorted each group by insertion (of course, this can also be done, but the subscript is not easy to control), but this is a wrong understanding of the following code.
The correct process is as follows: the outer for loop inserts and sorts the first two data items in each group, then the first three data items, and then the first four data items... this is related to the number of subsequences.
The sorting process is only performed by square brackets.
When out = 4, perform the following process ([0 4] 8)
When out = 5 ([1 5] 9)
When out = 6 ([2 6])
When out = 7 ([3 7])
When out = 8 ([0 4 8])
When out = 9 ([1 5 9])
H = 4 after the execution is completed, then start the new for loop with H = (h-1)/3 = 1
When H = 1, the execution process is the same as when H = 4. However, at this time, the sub-series are the original series, which is transformed into a simple insertion sorting. This is the basic order of the array, the number of moving data items is greatly reduced */

Int inner = 0, outer = 0;
Long temp;

For (outer = H; outer <items; outer ++) {// the outer layer determines the second data item sorted by insertion through the out clause.
// The following code is the insertion Sorting Algorithm for subsequences.
Inner = outer;
Temp = data [outer];

/*
* Compare the writing of insert sort while loop. The while loop here is related to H, so the determination is related to H, including the in-= H Statement.
* While (in> 0 & array [in-1]> TMP ){
* Array [in] = array [in-1];
* In --;
*}
* Array [in] = TMP;
*
*/
While (inner> h-1 & Data [inner-H]> temp)
{
Data [inner] = data [inner-H];
Inner-= h;
}

Data [inner] = temp;

}

H = (h-1)/3;

}

}

Main method File

# Include <iostream>
# Include <math. h>
# Include "arraysh. H"

Using namespace STD;

Int main (void ){

Arraysh as (10 );

For (INT I = 0; I <10; I ++)
{
Int d = rand ();
As. insert (d );
Cout <"RAND num is:" <D <Endl;

}

As. shellsort ();
As. Display ();

Return 1;
}

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.