[Game development-Learning notes] Rookie slowly Fly (vi)-bubble sort

Source: Internet
Author: User

1         /// <summary>2         ///Bubble Sort Algorithm3         /// </summary>4         /// <param name= "Array" ></param>5          Public Static voidSortint[] arr)6         {7             inttemp =0;8              for(inti =0; I < arr. Length-1; i++)9             {Ten                  for(intj =0; J < arr. Length-1I J + +) One                 { A                     if(Arr[j] > arr[j +1]) -                     { -temp = Arr[j +1]; theArr[j +1] =Arr[j]; -ARR[J] =temp; -                     } -                 } +             } -}

Xiao Kee:

      • I < arr. Length-1, outer loop, last comparison: Last digit (not moved) VS second-to-last number (possibly swapped); The final number is compared, so the number of cycles is "arr." Length-1 ";
      • J < arr. Length-1-I, Inner loop,
        • The first cycle (i=0), than only the end of the pair, so the number of cycles to "arr." Length-1 "
        • The second cycle (I=1), the end of the pair has been compared, do not need to compare, we compare to the penultimate group so far, so J cycle times to "arr." Length-1-1 "
        • The third cycle (i=2), at the end of the two pair has been compared, we compare to the last third group so far, so J cycle times to "arr." Length-1-2 "
        • The analogy concludes "J < arr. Length-1-I ";

[Game development-Learning notes] Rookie slowly Fly (vi)-bubble sort

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.