Sorting Algorithm-shell sorting

Source: Internet
Author: User

Shell sorting is a form of change in insert sorting. Because insert sorting compares each element and compares the current element with all the previous elements, in the worst case. Therefore, DL. shell proposed shell sorting in 1959, that is, grouping insertion sorting, grouping an array to be sorted, and inserting and sorting corresponding elements between multiple groups, you can add group values to sort the array. Therefore, shell sorting is also called incremental insert sorting. I thought about this sort and thought about it for two days. So we recorded this process.

If the array to be sorted is: 10, 14, 73, 25, 23, 13, 27, 94, 33, 39, 25, 59, 94, 65, 82, 45. N = 16;

1. We can set a group gap, Gap = n/2. Then this array is divided into two groups.

10, 14, 73, 25, 23, 13, 27, 94,

33, 39, 25, 59, 94, 65, 82, 45

In this case, 10 and 33 are compared, 14 and 39 are compared, and 73 and 25 are compared. Comparison between 25 and 59 .... The following comparison is performed in sequence. When the gap is 8, the sorting result is:

10 14 25 25 23 13 27 45

33 39 73 59 94 65 82 94

2. Now we are adding gap, Gap = N/4 = 4. Then this array is divided into four groups.

10 14 25 25

23 13 27 45

33 39 73 59

94 65 82 94

This comparison is the comparison. Comparison Between, and. The sorting result is as follows:

10 13 25 25

23 14 27 45

33 39 73 59

94 65 82 94

3. Now we are adding gap, Gap = N/8 = 2. Then this array is divided into eight groups for sorting, and finally until Gap = 1, that is, normal insertion sorting.

With the above process, our code is easier to write.

1 first, define the gap value, and then reduce the gap value each time to increase the number of groups. For (INT Gap = n/2; Gap> 0; Gap/= 2)

2. Compare and sort the values of each group. By default, we select an element from the first gap until the last element of the array. We can add or subtract the gap value from the index of the selected element to compare each element.

For (INT I = gap; I <n; I ++ ){

For (Int J = I-gap; j> = 0 & A [J]> A [I + Gap]; j-= gap)

Swap (A, J, J + gap );

}

Let's take a look at the complete code.

package com.bplead.sort;public class ShellSort {public static void main(String[] args) {int a[] = {10,14 ,73 ,25 ,23 ,13 ,27 ,94, 33, 39 ,25 ,59, 94, 65, 82, 45};int size = a.length;int temp = 0;for(int gap = size/2;gap>0;gap/=2){for(int i=gap;i<size;i++){for(int j=i-gap;j>=0&&a[j]>a[j+gap];j-=gap){temp = a[j];a[j] = a[j+gap];a[j+gap] = temp;}}print(a);}print(a);}private static void print(int a[]){for(int i=0;i<a.length;i++)System.out.print(a[i] +" ");System.out.println();}}

The calculation result is the same as what we analyzed previously, as shown below:

10 14 25 25 23 13 27 45 33 39 73 59 94 65 82 94
10 13 25 25 23 14 27 45 33 39 73 59 94 65 82 94
10 13 23 14 25 25 27 39 33 45 73 59 82 65 94 94
10 13 14 23 25 25 27 33 39 45 59 65 73 82 94 94
10 13 14 23 25 25 27 33 39 45 59 65 73 82 94 94

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.