Java Algorithm sorting-hill sorting

Source: Internet
Author: User

The hill sort method (scale-out increment method) is a sort of insertion class, which divides the entire unordered column into several small subsequences for insertion sorting respectively. For example, in the sorting process, take a positive integer d1 <n, put all array elements with serial numbers separated by d1 in a group, insert them directly into the group for sorting, and then take d2 <d1, repeat the above grouping and sorting operations until di = 1, that is, all records are put into a group for sorting until the initial: d = 549 38 65 97 76 13 27 49*55 0449 13 | --------------------- | 38 27 | ------------------- | 65 49 * | ------------------- | 97 55 | ------------------- | 76 04 | take a trip result 13: 27 49*55 04 49 38 65 97 76d = 313 27 49*55 04 49 38 65 97 7613 55 38 76 | ------------ | 27 04 65 | ------------ | --- --------- | 49*49 97 | ------------ | Result 13 04 49*38 27 49 55 65 97 76d = 113 04 49*38 27 49 55 65 97 76 | ---- | ---- | result 04 13 27 38 49*49 55 65 76 97 the algorithm IDEA is briefly described in the directly inserted sorting algorithm., insert a number each time, so that the sequence can only add one node, and there is no help for inserting the next number. If a comparison is a long distance (called incremental) number that allows the number to move across multiple elements, then performing a comparison may eliminate the exchange of multiple elements. D. L. shell implemented this idea in the sorting algorithm named by him in 1959. The algorithm divides the number of groups to be sorted into several groups based on a certain increment d. The subscript difference between the records in each group is d. sort all the elements in each group, and then use a small increment to sort them in each group. When the increment is reduced to 1, the entire number to be sorted is divided into a group, and the sorting is completed. The following function is an implementation of a hill sorting algorithm. The first half of the sequence is incremental, and the next half is halved until the increment is 1. Hill sorting is unstable. Edit the implementation method function in this section of C language. The following is a C language algorithm used to sort elements in an array, such as 503,17, 512,908,170,897,275,653,462,154,509,612,677,765,703, and 94: void shell_sort (int * x, int n) {int h, j, k, t; for (h = n/2; h> 0; h = h/2) /* control increment */{for (j = h; j <n; j ++) /* this is actually the preceding direct insertion sorting */{t = * (x + j); for (k = j-h; (k> = 0 & t <* (x + k); k-= h) {* (x + k + h) = * (x + k );} * (x + k + h) = t ;}} void main () {# define MAX 16int * p, I, a [MAX]; /* Input test data * // * p = a; printf ("Input % d number for sorting: \ n", MAX); for (I = 0; I <MAX; I ++) {scanf ("% d", p ++);} * You can input data by yourself */a [] = {503,17, 512,908,170,897,275,653,462,154,509,612,677,765,703, 94 }; printf ("\ n"); // 503,17, 512,908,170,897,275,653,462,154,509,612,677,765,703, 94/* test order */p = a; shell_sort (p, MAX ); /**/for (p = a, I = 0; I <MAX; I ++) {printf ("% d", * p ++ );} printf ("\ n"); system ("pause");} pascal Algorithm program: program xepx; const n = 7; type arr = array [1 .. n] of integer; var a: arr; I, j, t, d: integer; bool: boolean; beginwrite ('input data: '); for I: = 1to n do read (a [I]); writeln; d: = n; while d> 1 do begind: = d div 2; forj: = d + 1 ton do begint: = a [j]; I: = j-d; while (I> 0) and (a [I]> t) do begina [I + d]: = a [I]; I: = I-d; end; a [I + d]: = t; end; write ('output data: '); fori: = 1ton dowrite (a: 6); writeln; end. C ++ sample code: template <typename RandomIter, typename Compare> void shell_sort (RandomIter begin, RandomIter end, Compare cmp) {typedef typename std: pair <RandomIter> :: value_type; typedef typename std: iterator_traits <RandomIter >:: difference_type diff_t; diff_t size = std: distance (begin, end); diff_t step = size/2; while (step> = 1) {for (diff_t I = step; I <size; ++ I) {value_type key = * (begin + I); diff_t ins = I; while (ins> = step & cmp (key, * (begin + ins-step) {* (begin + ins) = * (begin + ins-step ); ins-= step;} * (begin + ins) = key;} if (step = 2) step = 1; else step = static_cast <diff_t> (step/2.2) ;}} template <typename RandomIter> void shell_sort (RandomIter begin, RandomIter end) {typedef typename std: Random <RandomIter>:: value_type; shell_sort (begin, end, std: less <value_type> ();} Java code implementation: // shell Sorting/** basic idea: divide the sequence into several categories, divide it into several groups in the class, and then sort it in the group, repeat until the sorting is successful * 1, and locate the gap (classification) the final spacing classification must be 1, the first cycle * 2, find the groups, the cycle between groups, the second cycle * 3, find the elements in the group, the third cycle, and dump */public static int [] ShellInsertSort (int [] data) {int [] temp = new int [data. length]; // store the result for (int gap = data. length/2; gap> = 1; gap --) // gap spacing {System. out. println ("gap =" + gap); int iter = 0; // traverses the array of the stored results; for (int I = 0; I <gap; I ++) // I is to traverse the same extraction group {boolean IsHead = true; // indicates whether it is the beginning of each Group int groupItemC = 0; // indicates the elements contained in each group. for (int j = I; j <data. length & iter <temp. length; j = j + gap, iter ++) // The transfer memory loop if (IsHead) // determines whether it is the start of the Group {groupItemC = 0; temp [iter] = data [j]; IsHead = false; groupItemC ++;} else {for (int groupiter = iter-groupItemC; groupiter <iter; groupiter ++) {if (data [j] <= temp [groupiter]) {for (int tempiter = iter; tempiter> groupiter; tempiter --) {temp [tempiter] = temp [tempiter-1];} temp [groupiter] = data [j]; break; // after saving, jump out of the loop and start to save the next element} if (groupiter = iter-1 & data [j]> temp [groupiter]) {www.2cto. comtemp [iter] = data [j] ;}} groupItemC ++ ;}} data = temp; for (int x: data) {System. out. print (x + "");} System. out. println (); temp = new int [data. length];} return data ;}

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.