The most detailed history of the C language and Python insertion sort algorithm


The most detailed history of the C language and Python insertion sort
insertion sort principle: the so-called insertion sort, just like when we play cards (landlords), organize our own hands, like his cards, like 2,1,3,9 , J, K, 5,4, these four cards.
we take it one of the few cards and then organized into 12345 such cards is not you have to put together these few cards, or to say which a card for the center, the other four were inserted by size.

 Without consent, please do not reprint! If receipt, please leave a like, be grateful!

    At the same time welcome to join us qq exchange group: 326 079 727

Man of few words said on the code: 
C Language:
. 1  void INSERT () {
 2      // DEFINE A Array Which have have Number Ten 
. 3      int init_array [ 10 ] = { . 8 , 2 , . 1 , . 9 , 77 , 55 , 66 , 42 is , . 3 , . 5 };
 . 4      int J;
 . 5      int len = the sizeof (init_array) / . 4 ; // Get the actual length of the array
 6      // from the second comparator before a start 
. 7      for ( int I =. 1 ; i <len; i ++ ) {
 . 8          int buffer = init_array [i];
 . 9          // here why it define a buffer
 10          // because the rear shift array elements to change the order, the same i, but init_array [i] becomes 
. 11          J = I- . 1 ;
 12 is          the while (J> = 0 && Buffer> init_array [J])
 13 is              // If there is a value smaller than init_array [j], here on the front row of the array good sequence after shifting
 14              // greater than the number is in descending order of 
15          {
 16              init_array [J + . 1 ] = init_array [J];
 . 17              J, ;
 18 is          }
. 19          init_array [J + . 1 ] = buffer;
 20 is          // the buffer passed init_array [j + 1], because init_array [j] is smaller than the number of buffer.
21          // descending order, so the buffer to put init_array [j + 1] below. 
22 is  
23 is      }
 24      for ( int K = 0 ; K <len; ++ K) {
 25          the printf ( " % D \ n- " , init_array [K]);
 26 is      }
 27 }
 

Python:

DEF INSERT (): 
    init_array = [2,3,5,1,22,99,44,6,7 ]
     for I in Range (. 1 , len (init_array)): 
        J = I. 1- 
        Buffer = init_array [I] # this buffer is important, why is it here to define a buffer because the back to move into the array to change the order, so i unchanged, but init_array [i] becomes 
        the while (J> = 0 and buffer> init_array [J] ): # here, too, compared with buffer, for the same reasons 
            init_array [J +. 1] = init_array [J] 
            J = J. 1- 
        init_array [J + 1'd] = Buffer
     Print (init_array) 
INSERT ()

 

Take pictures if infringement contact I removed

Guess you like

Origin www.cnblogs.com/szj666/p/11873170.html