Insert Sort basic idea
Each time a sorted record is inserted into the appropriate location of the previously sequenced sub-file by its keyword size until all records are inserted.
Direct Insertion Sort Basic idea
The basic operation of a direct-insert sort is to insert a record into an ordered, sorted table, resulting in a new ordered table. That is, suppose the records to be sorted are stored in the array r[1 N], during sorting, R is divided into two sub-intervals r[1 I] and r[i+1 n], wherein, r[1 I] is an ordered area that has been sequenced; R[i+1 N] is the part that is not currently sorted. Inserts the first record of the current unordered zone r[i+1] into the ordered area r[1 I] to the proper position, so that the r[1 I+1] becomes a new ordered area, inserting one data at a time until all the data is in order.
Java programs
/************************** * 直接插入排序**************************/public class InsertSort {private void insertSort(int[] datas) { if (datas == null || datas.length < 2) return; int i, j, insertData; for (i = 1; i < datas.length; i++) { insertData = datas[i];// 要插入的变量 for (j = i - 1; j >= 0 && insertData < datas[j]; j--) datas[j + 1] = datas[j]; datas[j + 1] = insertData;// 将要插入的数据放置到正确的位置 }}public static void main(String[] args) { int[] datas = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 }; System.out.println("********排序前********"); for (int i = 0; i < datas.length; i++) { System.out.print(datas[i] + ","); } InsertSort insertSort = new InsertSort(); insertSort.insertSort(datas); System.out.println("\n********排序后********"); for (int i = 0; i < datas.length; i++) { System.out.print(datas[i] + ","); }}}
Performance analysis
- Complexity of Time
- The direct insert sort is an in-place sort and is a stable sort method.
Basic idea of Hill sort
First take an integer less than n D1 as the first increment, the entire record of the file is divided into D1 groups, all the records with multiples of D1 are placed in the same group, the insertion sort is made within each group, and then the second increment D2 < D1, repeating the above groupings and sorts until the obtained increment dt = 1 (dt < Dt-1 < < D2 < D1), that is, all records are placed in the same group for direct insert sorting.
Java programs
/************************** * Hill sort **************************/public class Shellsort {private void Shellsort (int[] Datas) {if (datas = = NULL | | Datas.length < 2) return; int temp;//temporary variable int datalength;//step int pointer;//The position to be processed datalength = datas.length/2;//initialization step while (DAT Alength! = 0) {for (int j = datalength; J < Datas.length; J + +) {temp = Datas[j]; pointer = j-datalength; while (pointer >= 0 && temp < Datas[pointer]) {datas[pointer + datalength] = Datas[pointer]; pointer = pointer-datalength; } Datas[pointer + datalength] = temp; } datalength = DATALENGTH/2; }}public static void Main (string[] args) {int[] datas = new int[] {6, 5, 3, 1, 8, 7, 2, 4}; System.out.println ("******** ******** before sorting"); for (int i = 0; i < datas.length; i++) {System.out.print (Datas[i] + ","); } Shellsort Shellsort =New Shellsort (); Shellsort.shellsort (datas); System.out.println ("\n******** ******** after sorting"); for (int i = 0; i < datas.length; i++) {System.out.print (Datas[i] + ","); }}}
Performance analysis
The execution time of the hill sort depends on the selection of the increment sequence (step). A good increment sequence has the following characteristics:
- The last increment must be 1
- You should try to avoid situations in which the values in the sequence, especially the neighboring values, are multiplied by each other.
The time performance of the Hill sort is due to the direct insertion sort, for the following reasons:
- The number of comparisons and moves required to insert a sort directly when the initial state of the file is basically ordered is low.
- In the beginning of the hill, the increment is larger, more groups, fewer records per group, so the direct insertion within the group is faster, and then the incremental di gradually reduced, the number of groups gradually decreased, and the number of records of the group gradually increased, but because already according to Di-1 as a distance row order, so that the file is closer to the orderly state, So the new trip sort process is also faster. Therefore, the hill sort is more efficient than the direct insertion sort.
Hill sort is an unstable sort of method
Reference: "Data structure and algorithm analysis--java language description", "Big liar data Structure"
"Algorithm supplements (Java description)"---insert sort (direct insert sort, hill Sort)