[Algorithm retrieval (java description)] --- insert sort (insert sort directly, Hill sort)
Basic Idea of insert sorting
Each time a record to be sorted is inserted to the appropriate position of the subfile in the previous order according to its keyword size until all records are inserted.
Basic Idea of direct insertion and sorting
The basic operation of directly inserting sorting records is to insert a record to an ordered table in the sorted order to obtain a new ordered table. Assume that the records to be sorted are stored in the array R [1 · n]. During the sorting process, R is divided into two subintervals: R [1 · I] and R [I + 1 · n]. Among them, R [1 · I] is an ordered area already sorted; R [I + 1 · n] is an unordered part. Insert the first record R [I + 1] of the current unordered zone to the appropriate location of the ordered zone R [1 · I, change R [1 · I + 1] to a new ordered zone. Insert a data record each time until all data is sorted.
Java program
********* * ***************/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]; // The variable to be inserted for (j = I-1; j> = 0 & insertData <datas [j]; j --) datas [j + 1] = datas [j]; datas [j + 1] = insertData; // place the data to be inserted in the correct position} 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 ********* after sorting ********"); for (int I = 0; I <datas. length; I ++) {System. out. print (datas [I] + ",");}}}
Performance analysis time complexity
Direct insertion sorting is a stable sorting method. Basic Thinking of hill sorting
Take an integer d1 less than n as the first increment, divide all the records of the file into d1 groups, and put all the records that are multiples of d1 in the same group, insert and sort data in each group. Then, the second incremental d2 <d1 is used to repeat the preceding grouping and sorting, until the obtained incremental dt = 1 (dt <dt-1 <· <d2 <d1), that is, all records are placed in the same group for direct insertion sorting.
Java program
********** * **************/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 processing position dataLength = datas. length/2; // initialize the step while (dataLength! = 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 ("**********"); 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 hill sorting depends on the incremental sequence (step size) selection. A good incremental sequence has the following features:
The last increment must be 1. Try to avoid the mutual multiples of values in the sequence (especially adjacent values.
The time performance of hill sorting is due to direct insertion sorting, for the following reasons:
When the initial State of a file is basically ordered, the comparison and moving times required for direct insertion sorting are relatively small. At the beginning of the hill sorting, there were a large increase in the number of groups and the number of records in each group was small. Therefore, direct insertion in each group was faster. Later, the incremental di gradually reduced and the number of groups gradually reduced, the number of records in each group gradually increased, but because the di-1 has been used as the distance sorting order, the file is closer to the orderly state, so the new sorting process is also faster. Therefore, the efficiency of hill sorting is much higher than that of direct insertion sorting.
Hill sorting is an unstable sorting method.
References: Data Structure and algorithm analysis-java language description and big talk Data Structure