Hill sort (Shell sort)

Source: Internet
Author: User

This sort looks more difficult than bubbling selection, inserting, and logically complicated.

Intuitively, I know this is not simple, a little afraid.

There is no way to give yourself a hint of psychology, be sure to read the 8 basic sorting algorithm, slowly come.

Java Code Implementation:


public static void Shell_sort (int array[],int lenth) {
int temp = 0;
int incre = Lenth;
while (true) {
Incre = INCRE/2;
for (int k = 0;k<incre;k++) {
for (int i=k+incre;i<lenth;i+=incre) {
for (int j=i;j>k;j-=incre) {
if (Array[j]<array[j-incre]) {
temp = Array[j-incre];
Array[j-incre] = Array[j];
ARRAY[J] = temp;
}
else{
Break
}
}
}
}
}
}

Let's get the code out for reference.

Overall thought:

In the set of numbers to be sorted, a number of sub-sequences are divided according to an increment, and the sub-sequences are sorted separately.
Then gradually decrease the increment and repeat the process. Until the increment is 1, the data sequence is basically orderly, and the final insertion sort is done.

Do not understand, what increment, this concept is not clear. Then I understand it is a number, because the number has a size, then there is an increment, increment is a number increase how much.


For my own part: In an array, group, and then sort by insert for each group. How to group it, is to start half points, for each group and half points just. This is what he said about the increment,

Then there's only one number in the group, so it can't be grouped.

Now the array is basically nine ordered,

Finally, come on. Insert Sort


Design steps:

1. Method accepts an array, a length. This length is the increment, the shell sort, is the incremental split sort.

2. It is really not the reason to give up, it is loading the code into the brain may be caused by short-circuiting and so on.

This while (true) I can not think of, now I copy down and what use, wait until I write my own time and unexpectedly, only by reciting, certainly can't back out. I've been on my back and my brain is empty

3, according to the basic idea, in the group there is a circular grouping of ideas, here may be used in the notation of the while loop, when the exit, that is, this increment equals one.

4. Increment start is the number of incoming values, each trip to the loop, the increment is the addition of 2.

5. for (int k = 0;k<incre;k++) {
for (int i=k+incre;i<lenth;i+=incre) {
for (int j=i;j>k;j-=incre)

What do these nested loops mean and don't understand.


Online people write what ideas are completely useless, this I think must be separated step by step, the idea is false, only separate step by step, others follow separate step by step go down no problem. I'm stuck here in 5.

It is necessary to explain why these loops are. In order to achieve what purpose.

My own train of thought is that you must loop apart the left and right sides of the array.

It's a little bit of digital.

Increae is 5.

Then length is 10

for (int k = 0;k<5;k++) {
for (int i=k+5;i<10;i+=5) {
for (int j=i;j>k;j-=5)

It is understood that:

The outermost 0 to 5 loops, then inside are 5 to 101 loops. If the variable is the left and right side of an array

Then inside a loop is from 10 to 5 to judge the number of Array[j]<array[j-incre] 10 to 5, the back is smaller than the previous exchange, not the next comparison.

The array of the two or so sides is all over.

The thought is, I am. The left and right sides of an array are looped through.


This multi-layered loop seems to be too complex to be logical.

I'm still not clear, FACK.


Related Article

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.