Sort by bubble

Source: Internet
Author: User
1   //  Bubble Sorting  2   /*  ******************AlgorithmDescription **********************  3   1. traverse the unordered sequence and compare the number with the next one. If the number is larger than the next, exchange  4   2. Loop 1 until the length (called the number of workers,  5   Supplement 1: If there is no exchange in any of the switches, the sorting is successful.  6   Supplement 2: because the maximum value is placed at the end, the efficiency should be less than once. 7   **************************************** ********  */  8   Void Sortbybubble ( Int Array [], Int  Length)  9   {  10       Int  Itemp;  11       //  2. Loop 1 until the length (called the number of workers, 12       For ( Int I = 0 ; I <length; I ++ )  13   {  14           //  1. traverse the unordered sequence and compare the number with the next one. If the number is larger than the next, exchange  15           For ( Int J = 0 ; J <length- 1 -J; j ++)  16   {  17               //  Compare the number with its next number.  18               If (Array [J]> array [J + 1 ]) //  If this number is greater than the lower number  19   {  20 Itemp = Array [J];  21 Array [J] = array [J + 1  ];  22 Array [J + 1 ] = Itemp;  23   }  24   }  25   }  26   }  27   28   Void Print ( Int A [], Int  Length)  29   {  30       For ( Int I = 0 ; I <length; I ++ )  31   {  32 Printf ( "  % D  " , A [I]);  33   }  34 Printf ( "  \ N  "  );  35   }  36   37   Int  Main ()  38   {  39       Int A [ 10 ] = { 7 , 2 , 8 , 3 , 9 , 10 , 15 , 11 , 20 , 17  };  40 Printf ( " Bubble Sorting: \ n  "  );  41 Sortbybubble (, Sizeof ()/ Sizeof ( Int  ));  42 Print (, Sizeof ()/ Sizeof ( Int  ));  43       Return  0  ;  44 }

 

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.