Insert Sort : Each trip adds a sorted record to the appropriate location of the ordered queue according to the size of its keyword, knowing that all insertions are complete.
Before we explain the direct insertion of sorting, let's head up the process of playing cards.
Get a 5 in your hand first.
And then touched a 4, smaller than 5, and inserted into the 5 front,
Touch a 6, well, bigger than 5, plug it into 5 behind,
Touch a 8, bigger than 6, and plug in 6 behind,
。。。
The above process, in fact, is a typical direct insert sort, each time a new data is inserted into an orderly queue in the appropriate location .
Next, we'll turn this algorithm into a programming language.
Suppose there is a set of unordered sequences R0, R1, ..., RN-1.
(1) We first treat the element labeled 0 in this sequence as an ordered sequence with a number of 1 elements.
(2) Then we have to put R1, R2, ..., RN-1 into this ordered sequence in turn. So, we need an external loop that scans from subscript 1 to N-1.
(3) Next describe the insertion process. Suppose this is to insert an Ri into an ordered sequence in front of you. As mentioned earlier, we know that when you insert an RI, the number of first i-1 is definitely already in order.
So we need to compare the Ri and R0 ~ Ri-1 to determine the appropriate location to insert. This requires an internal loop , which we generally compare from backward to forward, that is, from the bottom
Main code
Public voidInsertsort (int[] list) { //Print the first elementSystem.out.format ("i =%d:\t", 0); Printpart (list,0, 0); //The 1th number must be orderly, traversing from the 2nd number, inserting an ordered sequence sequentially for(inti = 1; i < list.length; i++) { intj = 0; inttemp = List[i];//Remove the number of I, compared with the number of previous i-1, insert the appropriate position//since the first i-1 number is ordered from small to large, so long as the number of current comparisons (List[j]) is larger than the temp, move the number back one for(j = i-1; J >= 0 && temp < list[j]; j--) {list[j+ 1] =List[j]; } list[j+ 1] =temp; System.out.format ("I =%d:\t", i); Printpart (list,0
PackageNotes.javase.algorithm.sort; ImportJava.util.Random; Public classInsertsort { Public voidInsertsort (int[] list) { //Print the first elementSystem.out.format ("i =%d:\t", 0); Printpart (list,0, 0); //The 1th number must be orderly, traversing from the 2nd number, inserting an ordered sequence sequentially for(inti = 1; i < list.length; i++) { intj = 0; inttemp = List[i];//Remove the number of I, compared with the number of previous i-1, insert the appropriate position//since the first i-1 number is ordered from small to large, so long as the number of current comparisons (List[j]) is larger than the temp, move the number back one for(j = i-1; J >= 0 && temp < list[j]; j--) {list[j+ 1] =List[j]; } list[j+ 1] =temp; System.out.format ("I =%d:\t", i); Printpart (list,0, i); } } //Print Sequence Public voidPrintpart (int[] list,intBeginintend) { for(inti = 0; i < begin; i++) {System.out.print ("\ T"); } for(inti = begin; I <= end; i++) {System.out.print (List[i]+ "\ T"); } System.out.println (); } Public Static voidMain (string[] args) {//initialize a random sequence Final intMax_size = 10; int[] Array =New int[max_size]; Random Random=NewRandom (); for(inti = 0; i < max_size; i++) {Array[i]=Random.nextint (max_size); } //call the bubbling Sort methodInsertsort Insert =NewInsertsort (); System.out.print ("Before sorting: \ t"); Insert.printpart (Array,0, Array.length-1); Insert.insertsort (array); System.out.print ("After sorting: \ t"); Insert.printpart (Array,0, Array.length-1
Run results
Before sorting:6 3 3 5 6 3 1 0 6 4I=0:6I=1:3 6I=2:3 3 6I=3:3 3 5 6I=4:3 3 5 6 6I=5:3 3 3 5 6 6I=6:1 3 3 3 5 6 6I=7:0 1 3 3 3 5 6 6I=8:0 1 3 3 3 5 6 6 6I=9:0 1 3 3 3 4 5 6 6 6after sorting:0 1 3 3 3 4 5 6 6 6
Run Results
Direct Insert Sort 3