Algorithm description: Usually people in the process of organizing poker is one card, each card inserted into other already ordered in the appropriate position. In the implementation of the algorithm, in order to make 1 space for the element to be inserted, we need to move all the remaining elements to the right 1 bits before inserting. This algorithm is called the insertion algorithm.
Algorithm diagram:
Algorithmic interpretation: In the basic version it is common practice to swap from right to left when the new element needs to be inserted into an ordered array. Until the new element reaches its proper position.
Java code example:
ImportCommon. Arraysgenerator;ImportCommon. sortable;Importjava.io.IOException;Importjava.util.Arrays; Public classInsertionImplementsSortable<integer>{@Override Public voidsort (integer[] array) { for(inti = 1; i < Array.Length; ++i) { for(intj = i; J > 0; --j) {//if array Array[index] < Array[index-1] Swap location if(Array[j] < array[j-1]) { intTMP =Array[j]; ARRAY[J]= Array[j-1]; Array[j-1] =tmp; } } } } Public Static voidMain (String arg[])throwsIOException {//static method for reading an array from a file, specifying file names and arrays of lengthsinteger[] arr = arraysgenerator.fromfile ("Disorder.txt", 1000000); Insertion Insertion=Newinsertion (); LongStart =System.currenttimemillis (); Insertion.sort (arr); LongEnd =System.currenttimemillis (); System.out.println (arrays.tostring (arr)); System.out.println (End-start); }}
Qt/c++ code Example:
voidInsertion::sort (int* Arr,intLen) { for(inti =1; i < Len; ++i) { for(intj = i; J >0; --j) {if(Arr[j] < arr[j-1]) { intTMP =Arr[j]; ARR[J]= Arr[j-1]; Arr[j-1] =tmp; } } }}
Algorithm improvement: Still consider people to tidy up the situation of poker, when need to insert the time and only do a single operation and when the new card does not need to organize the time to touch a hand directly. Therefore, it is not difficult to significantly increase the speed of the insertion sort by simply moving the larger elements to the right in the inner loop and not exchanging the positions of the two elements.
Algorithm diagram:
Java code example:
import Common. sortable; Public classInsertionimprove Implements Sortable<integer>{@Override Public voidsort (integer[] array) { for(inti =1; i < Array.Length; ++i) {//if Array[index] is greater than the last 1 bits of the ordered array Array[0-index) if(Array[i] > Array[i-1]) { Continue; } for(intj = i; J >0; --j) {//if present array[j-1] <= Array[index] < Array[j] if(Array[i] < Array[j] && Array[i] >= array[j-1]) { intTMP =Array[i]; //array array[j-i) moves backwards by 1 bits for(intn = i; n > J; --N) {Array[n]= Array[n-1]; } Array[j]=tmp; Break; } } } }}
Qt/c++ code example (slightly)
In general, the insertion sort is very funny for partially ordered arrays and is good for small-scale arrays. This is important because these types of arrays often appear in real-world applications, and they are also intermediate processes in advanced sorting algorithms.
RELATED links:
Algorithms for Java
Algorithms for Qt
Little Orange Book Reading Guide (iii)--insert Sort