Insert sorting and Hill sorting
Insert sorting:
Field definition:
Sorting: data is divided into order and disorder, so that data is sorted from disorder to order. The rules used are called sorting algorithms.
Time Complexity: The algorithm does not have a specific time unit. O is used to represent the upper bound of an algorithm (worst case ).
Algorithm Description:
Starting from the first element, this element can be considered to have been sorted to retrieve the next element. In the sorted element sequence, it is scanned from the back to the front. If the element (sorted) is larger than the new element, move the element to the next position and repeat Step 3 until the sorted element is found to be smaller than or equal to the new element, insert the new element to this position, and then repeat Step 2 ~ 5
Programming implementation (java ):
public void Insertsort(int []a){ int j = 0; for (int i = 1; i < a.length; i++){ int temp = a[i]; for (j = i; j > 0 && a[j-1] > temp; j--) a[j] = a[j-1]; a[j] = temp; } }
Tips: It is generally considered that the time complexity of the algorithm for sorting by exchanging adjacent elements is O (N2), so insertion sorting is naturally the same.
Hill sorting: Field definition:
Incremental sequence: sequence h1, h2, h3 ......, Hn, satisfying h1 =
Algorithm Background: The name of hill sorting comes from its inventor, Donald Shell. This algorithm is the first outstanding representative to break through the sorting time complexity O (N2. It greatly improves the efficiency of regular insert sorting, because regular insert sorting can only move one bit of data at a time.
Algorithm Description:
Defines the incremental series to compare the sizes of a [I] And a [I + gap] based on the incremental values and sort them. Repeat Step 3 until the incremental value is gap from I = gap to. the sorting of length repeats step 3 and 4 until all incremental values are sorted. When the value of the last step is 1, the sorting of standard insertion is completed. Example:
Programming implementation (java ):
public void shellSort(int []a){ int j = 0; for (int gap = a.length >> 1; gap > 0; gap = gap >> 1){ for (int i = gap; i < a.length; i++){ int temp = a[i]; for (j = i; j >= gap && a[j-gap] > temp; j = j - gap) a[j] = a[j-gap]; a[j] = temp; } } }
Tips: Use the incremental sequence of Hibbard (1, 3, 7 ,....., 2 k-1) The Worst run time for implementing the hill sorting is merge (N3/2 ).
Efficiency Comparison:
Cpu: msm8930 (dual core 1.5G)