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