The example of this article tells the Sort of Hill Summary of the Java Sorting Algorithm. Share it for everyone for your reference. The specific analysis is as follows:
Foreword : Shell Sort is a type of inserting sorting. It is aimed at the improvement of directly inserting the sorting algorithm. This method is also called reduced incremental sorting because DL. Shell was named in 1959. This article mainly introduces how Hill's sorting is achieved with Java.
Hill's sorting (reduction incremental method) belongs to the insertion class sorting. It is to divide the entire disorderly sequence into several small sub -sequences for inserting sorting. Hill's sorting is not stable. The extra space of O (1) has time complexity of O (n*(Logn)^2). In the worst case, the execution efficiency is much different from the implementation efficiency under the average situation.
Basic thoughts:
First take an integer D1 less than N as the first increment, and divide all the records of the file into the D1 group. The records of all distances D1 are placed in the same group. First insert and sort in each group; then, take the second incremental D2 <D1 to repeat the above group and sorting until the incremental increase DT = 1 (DT <DT-L <… <D2 <D1)) That is, all records are placed in the same group and are directly inserted.
Code implementation:
Public class test {public static int [] a = {10, 32, 1, 9, 5, 7, 0, 4, 3}; // Preset data array Public Static void Main (String ARGS []) { int i; // Cycling count variable int index = a.length; // Data index variable system.out.print ("" before sort: "); for (i = 0; i </+) system.out .printf ("%3s", a); System.out.println (""); shellSort (index -1); // Select systerm.out.print after selecting // for (i = 0; I <inDex -1; I ++) System.out.printf ("%3s", a); System.out.println (""); , j, k; // cycle counting variable int temp; // temporarily stored variable Boolean Change; // Whether the data changes int dataLength; // The interval length of the segmentation set int points; // Index / 2; // Initial collection interval length while (dataLength! = 0) // The number of columns can still be divided {// to process for (j = dataLength; j <in ++) {Change = false; = a [j]; // Temporarily deposit the value of data [j], use point = j -dataLength; // Calculate the position of the set value in the set value of the exchange value when the exchange value is to be exchanged. a [pointr] && pointer> = 0 && pointer <= Index) {a [pointr + dataLength] = a [pointer]; // Calculate the next location of the process. Pointer <0 || Pointer> Index) Break;} // Exchange with the final value exchange a [pointr + dataLength] = TEMP; if (Change) {// Print the current ranking systers.out.print ("Sorting:" "" "" "" "" "" "" "" "" " ); free (k = 0; k <inndex; k ++) system.out.printf ("%3s", a [k]); system.out.println ("");} dataLength = dataLength / 2; / / Calculate the interval length of the next segmentation}}}Hill sorted almost the worst situation. Whether it is positive, counter -order, and chaotic order, the time used is not a lot. The additional storage is O (1), which is indeed very good. It is indeed a good choice before it does not figure out fast sorting and stacking order. Hope to help you.
It is hoped that this article is helpful to everyone's Java program design.