Insertion sort (a) direct insertion sort

Direct insertion sort

 

  Each time a record to be sorted, according to the size of the keyword has been inserted in front of the sub-discharge well of an appropriate position in the table. Until all the records inserted completed.


Figure words, as shown:

  A total of N records, in list R R [0, n-1]

  In the sorting process at some point, there has been a scene if shown.

  among them:

    Pale green part already sorted ordered regions called R [0, i-1]

    Orange is the current element 

    Blue + orange partially disordered region has not been referred to sort R [i, n-1]

 

   We sort, always record the first disordered region, ordered regions into the proper position, so that the length of the +1 ordered regions, the formation of new ordered regions R [0, i]

 

  speak English:

    In fact, the very beginning, we assume an ordered list of areas that contain only one record. I.e., a first recording list R [0]

    All other records are located in a disordered region, i.e., R [1, n-1] are disordered regions.

    Start sorting:

      Take the first element from the ordered regions and disordered regions comparison last element, i.e., R [1] and R [0], and if R [1] is less than R [0], then R [0] is moved backward an occupied R [1] position. Original R [1] is inserted into R [0] the front. Formerly R [0] position. Length + 1 ordered regions; if R [1] is greater than R [0], two recording remains unchanged. Ordered zone length is still +1. Programming ordered regions R [0], R [1]

      After the comparison step through, sorting, now the first recording region becomes disordered R [2], and then R [2] proposed, and sequentially records it compares the foregoing, if the ratio R found [2] small recording, then R & lt [2] is inserted into the back of the recording. A case is assumed: R [2] and the first R [1], it is found R [2] is greater than R [1] will then be R [1] a backward movement, note: do not rush to the R [2] inserted in front of R [1] is. Because you do not know the relationship between R [0] and R [2] of. So, to continue with R [2] and R [0] comparison, assuming R [0] is less than R [2], then this time, then R [2] placed into R [0] behind i.e. R [1 ]s position.

      。。。。

      Until all records are inserted in such a pass.

    

 

  It can be understood, there is a platform, above all aligned doll (left rows). And a pair of chaotic doll (right circle), no mess stacked together. You operate the robot head, each picked up a lot from that of the right doll inside, and then move to the left. On the platform, each saw a doll, so that will be more on the mechanical doll dolls hands tucked platform and now you see, if you find a larger doll on the platform, then continue to the left to move the robot, look up and down the platform a doll, or if a large doll on the platform, then continue to the left to move the robot. . . Know you found robot doll doll large than on the platform (assuming called z), then it will be put back in the hands of a mechanical doll z's. Other than those of large dolls doll in your hand after you complete each comparison, have been moved some distance to the right. So, just to give you space to place out of the doll.

 

 

 

1    / * into disordered [i ... n] and ordered regions [1 ... i-1], a start ordered regions 0, the minimum value found from the disordered regions are put into the ordered regions in * / 
2      public   static  int [] insertsort ( int A []) {
 . 3          int I, J, index, tmp;
 . 4          / * 1. find the index index, point I, * / 
. 5          for (I = 0; I <A .length; I ++ ) {
 . 6              index = I;
 . 7              / * 2. then find whether there are smaller than the index value in the random region, if assigning the j index * / 
. 8              for (j = I; j < a.length; J ++ ) {
 . 9                  IF (A [index]> A [J])
 10                      index = J;
11              }
 12              / * 3. exchange index and the value of i * / 
13 is              tmp = A [index];
 14              A [index] = A [i];
 15              A [i] = tmp;
 16          }
 . 17          / * . 4 .. redirected index I * / 
18 is          index = I;
 . 19  
20 is          return   A;
 21 is  
22 is      }
View Code

 

Reprinted: https://www.cnblogs.com/jijizhazha/p/6118466.html

 

Guess you like

Origin www.cnblogs.com/trf19940627/p/11491144.html
Recommended