Mobile development-hill sorting parsing and Hill sorting Parsing

Source: Internet
Author: User

Mobile development-hill sorting parsing and Hill sorting Parsing

Basic Idea:
Hill sorting also becomes "downgrading incremental sorting". Its basic principle is that the array elements to be sorted are now divided into multiple sub-sequences, so that the number of elements in each sub-sequence is relatively small, then, the sub-sequences are directly inserted and sorted. After the entire "Basic Order" column is to be sorted, all elements are directly inserted and sorted. Therefore, we need to adopt the Skip splitting policy: to form a subsequence of records that are separated by a certain "increment, in this way, we can ensure that the result obtained after direct insertion and sorting in the sub-sequence is basic order rather than partial order. Hill sorting is the optimization and upgrade of directly inserted sorting algorithms.

The so-called basic order is that small keywords are basically in the front, and big keywords are basically in the back, not small, basically in the middle, such as {2, 1, 3, 6, 5, 8, 9 ,} it can be called Basic Order. But like {, 9, 3, 7, 8, 2, 4, 6}, 9 is in the third place, and 2 is in the third place.

Hill sorting is strictly based on the idea of insert sorting, which is also called to narrow down incremental sorting.
The specific process is as follows:
1. Divide an array containing n elements into n/2 arrays. the first data and n/2 + 1 data are a pair...
2. Compare and exchange each pair of data in order;
3. Divide the data into n/4 array sequences and sort them again;
4. Repeat the above process constantly. The sorting is completed as the sequence decreases and reaches 1.

If there is initial data: 25 11 45 26 12 78.
1. In the first round of sorting, the array is divided into 6/2 = 3 array sequences, 1st data and 4th data are one-to-one, 2nd data and 5th data are one-to-one, 3rd data items and 6th data items are one-to-one. Each data item is sorted by comparison.
The sorting order is [25, 11, 45, 26, 12, 78].

2. In the second round of sorting, the array after the last round of sorting is divided into 6/4 = 1 array sequence. At this time, the data is compared one by one, and the array is sorted by insertion sorting, the order is [11, 12, 25, 26, 45, 78].

When the last interval is 1, the hill sorting is insert sorting, but at this time the insert sorting is basically ordered, and there will be no movement of a large span of values (for example, there is a lot of data, but a small value suddenly appears)


Package ch08;/** Hill sorting */public class ShellSort {/*** sorting method */public static void sort (long [] arr) {// Initialize an interval int h = 1; // calculate the maximum interval while (h <arr. length/3) {h = h * 3 + 1;} while (h> 0) {// sort insertion long tmp = 0; for (int I = h; I <arr. length; I ++) {tmp = arr [I]; int j = I; while (j> h-1 & arr [j-h]> = tmp) {arr [j] = arr [j-h]; j-= h;} arr [j] = tmp;} // reduce the interval h = (h-1) /3 ;}}}

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.