Insert Sort basic idea
Each time a sorted record is inserted at its keyword size into the appropriate location of the previously sequenced sub-file until all records have been inserted.
Direct Insertion Sort Basic idea
The basic operation of a direct insert sort is to insert a record into an ordered, sorted table. Thus a new ordered table is obtained. That is, if 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], among them. 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 of the multiples of D1 are placed in the same group, the inserts are sorted within the groups; Take the second increment D2 < D1, repeating the above grouping and sorting 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 run time of the hill sort depends on the selection of the increment sequence (step). Good increment sequences have for example 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 example the following:
Hill sort is an unstable sort of method
Reference data: Data structure and algorithm analysis--java language description narrative, "Big talk data Structure"
"Algorithmic supplements (Java descriptive narrative)"---insert sort (direct insert sort, hill Sort)