Sorting allows us to actually develop one of the most commonly used algorithms, according to the sorting process in accordance with the principles of the internal sorting classification, it can be divided into the insertion of sorting, exchange sorting, selection of sorting, merge sort and other sorting methods. Let's first look at what the algorithms for inserting sorting are, and what their implementations are. The basic sorting idea of the insertion sort is to look at each of the elements to be sorted, insert each new element into the appropriate position in the previously sequenced sequence, and make the new sequence still an ordered sequence. There are three sorting methods in this category: Direct insert sort, binary insert sort, and hill sort.
1. Direct insertion sort A. Algorithm description
Direct insertion Sorting is one of the simplest methods of inserting sorting, and the basic idea is that the sequence of only one element is always ordered, so that the sequence of n records can be executed sequentially from the second element to the nth element, one at a time, to an ordered sequence of n elements. In general, inserting an element in an ordered sequence containing j-1 elements is a way to search for the position that should be inserted from the first J-1 element, and then move the element back when the insertion position is searched, so that the element can be inserted directly when the appropriate insertion position is found. In the case of keyword sequences {26, 53, 48, 11, 13, 48, 32, 15}, the process of inserting a sort directly is as follows.
B: Algorithm implementation
public void Insertsort (int[] r, Int. Low, int.) {for (int i = low + 1; I <= high; i++) if (compare (R[i], r[i-1])) { When less than, you need to insert r[i] into the ordered table int temp = R[i];r[i] = r[i-1];int j = i-2;for (; J >= Low && Compare (temp, r[j]); j--) r[ J + 1] = R[j]; Record move R[j + 1] = temp; Insert to the correct location}}
"Efficiency analysis"
Space efficiency: Only one sub-cell is used.
Time efficiency: Assuming that the number of elements to be sorted is N, the operation of inserting records one by one into an ordered table is n-1, each operation is divided into key code and moving record, and the number of comparisons and number of moves is determined by the initial arrangement of key codes for the sequence. The time complexity of direct insertion sorting is O (n²)
C: Implementation examples
Straightinsertionsort.java
Package Com.test.sort.insertion;public class Straightinsertionsort {/** * @param args */public static void main (string[] A RGS) {//TODO auto-generated method StubSystem.out.println ("Direct insert sort sort function Implementation" ") int[] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; Straightinsertionsort sort = new Straightinsertionsort (); SYSTEM.OUT.PRINTLN ("Sequence before sequencing:"); Sort.printarr (arr); Sort.insertsort (arr, 0, arr.length-1); System.out.println ("Sorted after sequence:"); Sort.printarr (arr);} public void Insertsort (int[] r, Int. Low, int.) {for (int i = low + 1; I <= high; i++) if (compare (R[i], r[i-1])) { When less than, you need to insert r[i] into the ordered table int temp = R[i];r[i] = r[i-1];int j = i-2;for (; J >= Low && Compare (temp, r[j]); j--) r[ J + 1] = R[j]; Record move R[j + 1] = temp; Insert to the correct position}}public boolean compare (int parama, int paramb) {if (Parama < Paramb) {return true;} else {return false;}} /** * Prints an array of elements */public void Printarr (int[] arr) {if (arr! = null) {for (int temp:arr) {System.out.print (temp + ""); }system.out.println ();}}}
D: Result output
2. Hill sort A. Algorithm description
Hill sort, also known as "narrowing the incremental sort", is a sort of sorting method that belongs to the insertion sort class, an improvement on the direct insertion sort, but has a great improvement in time efficiency. From the analysis of direct insertion sequencing, it is known that although the time complexity of the direct insertion sort is O (n²), the time complexity of the sequence of elements to be ordered can be increased to O (n). It can be concluded that the efficiency of direct insertion is greatly improved when the elements to be sorted are basically ordered. On the other hand, because the direct insertion sorting method is simple, the efficiency is higher when the n value is small. Hill sort is a sort of method to improve the direct insertion sort from these two points.
The basic idea of Hill sort is: firstly, the elements to be sorted are divided into several sub-sequences, so that the number of elements of each subsequence is relatively small, the sequence of each subsequence is directly inserted, and then the whole sequence is sorted by a direct insertion of all the elements after it is "basically ordered". According to the above-mentioned sorting idea, we give a sort of hill sort process:
⑴ Select a step sequence t1,t2,...,tk, where TI>TJ (i<j), tk=1;
⑵ by the number of step series K, the sequence of sequencing elements are treated as K-order;
⑶ each order, according to the corresponding step ti, the backlog sequence is divided into Ti sub-sequences, respectively, the sub-sequences are directly inserted into the order. When the step factor is 1 o'clock, all elements are treated as a sequence, with a length of N. Take the keyword sequence {26, 53, 67, 48, 57, 13, 48, 32, 60, 50} as an example, assuming that the selected step sequence is {5, 3, 1}, then the hill sort is shown in procedure 9-2. Because the length of the step sequence is 3, there is a total of 3 sequencing to treat the sort sequence. First, the keyword sequence is divided into 5 sub-sequences {26, 13},{53, 48},{67, 32},{48, 60},{57, 50}, respectively, and the results are sorted directly into the first order. Then, for the second Hill sort, where the step is 3, the keyword sequence is divided into 3 sub-sequences {13, 48, 53, 57},{48, 50, 67},{32, 26, 60}, which are directly inserted into the sorted result. Finally, a direct insertion of the entire sequence is ordered, at which point an ordered sequence of keywords is obtained, and the hill sort is finished.
B. Algorithm implementation
Public voidShellsort (int[] R,intLowintHighint[] Delta) { for(intk = 0; K < Delta.length; k++) Shellinsert (R, Low, High, delta[k]); //Direct Insert sort with step length of delta[k] } Private voidShellinsert (int[] R,intLowintHighintDeltak) { for(inti = low + Deltak; I <= high; i++) if(Compare (R[i], R[i-deltak])) {//R[i] should be inserted into the ordered table when it is less than inttemp =R[i]; intj = i-Deltak; for(; J >= Low && Compare (temp, r[j]); j = J-Deltak) R[j+ Deltak] = R[j];//record back shift [j];R[j + Deltak] = temp;//Insert to correct position } }
"Efficiency analysis"
Space efficiency: Only one sub-cell is used.
Time efficiency: Assuming that the number of elements to be sorted is N, the operation of inserting records one by one into an ordered table is n-1, each operation is divided into key code and moving record, and the number of comparisons and number of moves is determined by the initial arrangement of key codes for the sequence. The time complexity of direct insertion sorting is O (n²)
C. Using the sample
Hashinsertsort.java
Package Com.test.sort.insertion;public class Hashinsertsort {/** * @param args */public static void main (string[] args) {/ /TODO auto-generated Method StubSystem.out.println ("Hill sort Function Implementation" ""); int[] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; int[] Delta = {5,3,1}; Hashinsertsort sort = new Hashinsertsort (); SYSTEM.OUT.PRINTLN ("Sequence before sequencing:"); Sort.printarr (arr); Sort.shellsort (arr, 0, Arr.length-1,delta); System.out.println ("Sorted after sequence:"); Sort.printarr (arr);} public void Shellsort (int[] r, Int. Low, Int. High, int[] delta) {for (int k = 0; k < delta.length; k++) Shellinsert (R, Lo W, High, delta[k]); The direct insertion sort of a trip step of delta[k]}private void Shellinsert (int[] r, int low, int high, int deltak) {for (int i = low + Deltak; I < ; = high; i++) if (compare (R[i], R[i-deltak])) {///less than, you need to insert r[i] into the ordered table int temp = R[i];int J = i-deltak;for (; J >= Low && ; Compare (temp, r[j]); j = J-deltak) R[j + Deltak] = R[j]; Record post-move [J];r[j + deltak] = temp; Insert to the correct position}}public boolean compare (int parama, int paramb) {if (Parama < Paramb) {return true;} else {return false;}} /** * Prints an array of elements */public void Printarr (int[] arr) {if (arr! = null) {for (int temp:arr) {System.out.print (temp + ""); }system.out.println ();}}}
D. Results output
3. Binary insert sort a. Algorithm description
The direct insertion sorting algorithm is simple and easy to implement. When the number of elements to be sorted n is very small, this is a good sorting method, but usually the number of elements to be sorted n is very large, it is not appropriate to use the direct insertion sorting method, you need to improve the direct insertion sort. The basic operation of a direct insert sort is to insert an element into an ordered sequence, where the insertion position is determined by comparing the elements of an ordered sequence by the keyword. Since the insertion position is determined in an ordered sequence, the insertion position can be determined by continuously ordering the sequence of points, that is, the method of searching for the insertion position can be implemented using the binary lookup.
B. Algorithm implementation
Public voidBininsertsort (int[] R,intLowintHigh ) { for(inti = low + 1; I <= high; i++) { inttemp = R[i];//Save the element you want to insert inthi = i-1; intLo = low;//Set initial interval while(Lo <= hi) {//binary determining the insertion position intMid = (lo + hi)/2; if(Compare (temp, r[mid])) Hi= Mid-1; ElseLo= Mid + 1; } for(intj = i-1; J > Hi; j--) R[j+ 1] = R[j];//Moving ElementsR[hi + 1] = temp;//inserting elements}// for}
"Efficiency analysis"
Space efficiency: Only one sub-cell is used.
Time efficiency: Binary insertion Sorting only reduces the number of elements that are compared, but does not reduce the number of elements moved, binary the time complexity of the insertion sort is O (n²).
C. Examples of applications
Binaryinsertsort.java
Packagecom.test.sort.insertion; Public classBinaryinsertsort {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Binary Insert sort sort function implementation")); int[] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43 }; Binaryinsertsort Sort=NewBinaryinsertsort (); System.out.println ("Sort before sequence:"); Sort.printarr (arr); Sort.bininsertsort (arr,0, Arr.length-1); System.out.println ("Sort after sequence:");; Sort.printarr (arr); } Public voidBininsertsort (int[] R,intLowintHigh ) { for(inti = low + 1; I <= high; i++) { inttemp = R[i];//Save the element you want to insert inthi = i-1; intLo = low;//Set initial interval while(Lo <= hi) {//binary determining the insertion position intMid = (lo + hi)/2; if(Compare (temp, r[mid])) Hi= Mid-1; ElseLo= Mid + 1; } for(intj = i-1; J > Hi; j--) R[j+ 1] = R[j];//Moving ElementsR[hi + 1] = temp;//inserting elements}// for } Public BooleanCompareintParama,intparamb) { if(Parama <paramb) { return true; } Else { return false; } } /*** Print an array of elements sequentially*/ Public voidPrintarr (int[] arr) { if(Arr! =NULL) { for(intTemp:arr) {System.out.print (temp+ " "); } System.out.println (); } }}
D. Results output
Sequencing of data structures and algorithms (summary one)