Binary sorting-semi-insertion sorting

Source: Internet
Author: User
1   //  Binary semi-insertion sorting  2   /*  Half insertAlgorithmThoughts  3   1. Initialization: Set the ordered area as the first element, and set the disordered area as all subsequent elements.  4   2. Sequentially retrieve each element in the unordered Area  5   3. Search for the ordered area using the binary method and return the maximum number smaller than this number.  6   4. retain data at this location  7   5. Move the elements from this position to the last element in the ordered area. 8   6. Fill the location with the reserved data  9   */  10   Void Sortbyinserthalf ( Int Array [], Int  Arraysize)  11   {  12       Int  IMAX, imid, Imin;  13       For (Int I = 1 ; I <arraysize; I ++ )  14   {  15           Int Temp = Array [I];  16 IMAX = I- 1  ;  17 Imin = 0  ;  18           While (Imin <= IMAX)  19   {  20 Imid = (IMAX + Imin )/ 2  ;  21               If (Array [imid]> = Temp)  22   {  23 IMAX = imid- 1  ;  24  }  25               Else  26   {  27 Imin = imid + 1  ;  28   }  29   }  30           For ( Int J = I- 1 ; J> = IMAX +1 ; J -- )  31   {  32 Array [J + 1 ] = Array [J];  33   }  34 Array [IMAX + 1 ] = Temp;  35   }  36   } 37   38   Void Print ( Int A [], Int  Length)  39   {  40       For ( Int I = 0 ; I <length; I ++ )  41   {  42 Printf ("  % D  "  , A [I]);  43   }  44 Printf ( "  \ N  "  );  45   }  46   47   Int  Main ()  48  {  49       Int A [ 10 ] = { 7 , 2 , 8 , 3 , 9 , 10 , 15 , 11 , 20 , 17  }; 50 Sortbyinserthalf (, Sizeof ()/ Sizeof ( Int  ));  51 Print (, Sizeof ()/ Sizeof ( Int  ));  52       Return   0  ;  53 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.