Kids Learn data structure (9): Hill sort

Source: Internet
Author: User

Kids Learn data structure (9): Hill Sort (a) basic idea

Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.

(ii) examples

has an array whose original array is:

2-1.png

Take the initial increment gap = LENGTH/2 = 5 so that the entire array is divided into 5 groups (each group is represented by the same color)

2-2.png

The 5 groups of data are arranged in the order of small to large, respectively, and the result is

2-3.png

Narrow incremental gap = GAP/2 = 2, the entire array is divided into two groups

2-4.png

The two sets of data are arranged in the order of small to large, respectively, and the result is

2-5.png

Reduce the increment again, the entire array is divided into 1 groups

2-6.png

This set of data is sorted from small to large and the end result is

2-7.png (c) Code

1 C Language Implementation

#Include<stdio.h>voidSwap(int &a,int &b) {a ^= B; b ^= A; a ^= B;}voidShellsort(int a[],int n) {int I, j, Gap;for (GAP = n/2; Gap >0; Gap/=2) {for (i = gap; i < n; i++) {for (j = i-gap; J >=0 && A[j] > A[j + gap]; J-= Gap) {swap (A[j], a[j + gap]);}} }}IntMain(){int arr[] = {49,38,65,97,76,13,27,48,55,4};printf"Original array:");int i; int len = sizeof (arr)/sizeof (int); for (i = 0; i < len; i++) { printf ("%d", Arr[i]);} printf ("\ n"); Shellsort (arr, Len); printf ("Sorted array:"); for (i = 0; i < len; i++) { printf ("%d", Arr[i]);} printf ("\ n"); return 0;}                  

2 Java implementations

Import Java.util.Arrays;PublicClassSort {PublicStaticvoidShellsort(Int[] a) {int I, j, Gap;int len = a.length;for (gap = len/2; Gap >0; Gap/=2) {for (i = gap; i < Len; i++) {for (j = i-gap; J >=0 && A[j] > A[j + gap]; J-= Gap) {//Exchange two number a[j] ^= a[j + gap]; A[j + gap] ^= a[j]; A[j] ^= a[j + gap];} }}} public static void main (string[] args) { int[] arr = {49, 38, 65, 97, 76, 13, 27, 48, 55, 4}; System.out.println ( "Original array:" + arrays.tostring (arr)); Shellsort (arr); System.out.println ( "Sorted array:" + arrays.tostring (arr));}}     

3 Running Results

array: [49, 38, 65, 97, 76, 13, 27, 48, 55, 4]Sorted array: [4, 13, 27, 38, 48, 49, 55, 65, 76, 97]


Kids Learn data structure (9): Hill sort

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.