This article describes the Java sorting algorithm summary of hill sort. Share to everyone for your reference. The specific analysis is as follows:
Hill Sort (Shell sort) is a sort of insertion. is for the improvement of the direct insertion sort algorithm. The method is also called narrowing the incremental order, due to DL. The shell was named after it was introduced in 1959. This article mainly describes how Hill sorting Java is implemented.
The hill sort (narrowing increment method) is a sort of insert class, which divides the whole sequence into small sequences to be inserted. Hill sort is not stable, O (1) extra space, the time complexity of O (n (logn) ^2). In the worst case, the efficiency of execution is not much different from the efficiency of execution in the average case.
Basic idea:
First, take an integer d1 less than n as the first increment, dividing the entire record of the file into D1 groups. All records with a multiple distance of D1 are placed in the same group. A direct insertion sort is performed within each group, and then the second increment d2<d1 repeats the grouping and sorting above until the incremental dt=1 (DT<DT-L<...<D2<D1) is taken, where all records are placed in the same group for direct insertion sort.
Code implementation:
public class Test {public static int[] A = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; Preset data array public static void main (String args[]) {int i;//loop count variable int index = a.length;//Data index variable Syste
M.out.print ("Sort before:");
for (i = 0; i < Index-1 i++) System.out.printf ("%3s", a);
System.out.println (""); Shellsort (Index-1);
Select sort//Sort results after System.out.print ("after sort:");
for (i = 0; i < Index-1 i++) System.out.printf ("%3s", a);
System.out.println (""); public static void Shellsort (int Index) {int I, j, K;///Loop count variable int Temp;//transient variable boolean change;// Whether the data changes int datalength; The interval length of the partition set int pointer; The position of the processing is datalength = (int) INDEX/2; The initial set interval length while (datalength!= 0)//sequence can still be split {//The individual sets are processed for (j = datalength; J < Index;
J + +) {change = false; Temp = A[j]; The value of the staging data[j], which is to be exchanged with pointer = J-datalength; Calculate where to process
To compare and exchange values within a set while (Temp < A[pointer] && pointer >= 0 && pointer <= Index) {
A[pointer + datalength] = A[pointer];
Calculates the next desired position for processing pointer = pointer-datalength;
Change = true; if (pointer < 0 | |
Pointer > Index) break;
///With the final numerical Exchange A[pointer + datalength] = Temp;
if (change) {//print current sort result System.out.print ("in Sort:");
for (k = 0; k < Index; k++) System.out.printf ("%3s", a[k));
System.out.println (""); } datalength = DATALENGTH/2; Calculate the next split interval length}}
Hill sort has almost no worst-case scenario, either positive sequence, reverse order, random sequence, time spent is not much, additional storage is O (1), indeed very good. It's really a good choice before you get to the quick sort and heap sort. I hope I can help you.
I hope this article will help you with your Java programming.