Sort-bubble sort

Source: Internet
Author: User
Tags array length

Problem Solving steps:

    1. Use a For Loop (outer loop) to specify the number of laps to loop (by illustration, the number of laps in an array loop is 1)
    2. In each lap, a for loop (inner Loop) completes the adjacent element values in order to compare the large values to the subsequent elements
    3. The number of cycles per circle is determined by the cycle of the first circle. As shown

A) the number of inner loops is an array length-1 when the first loop element is compared

b) in the second loop element comparison, the inner loop number of the array length-2

c) and so on, it is concluded that the number of inner loops is an array length when the nth loop element is compared-n

//Bubble Sort Public Static voidBubblesort (int[] arr) {    //function//the outer loop is used to control the number of loops in the array     for(inti = 0; i < arr.length-1; i++) {        //J < Arr.length-1 to avoid the crossing of the corner Mark//J < Arr.length-1-i to compare efficiency and avoid repeated comparisons//The inner loop is used to compare the values of the elements and to swap the values of the large elements back         for(intj = 0; J < Arr.length-1-i; J + +) {            if(Arr[j] > arr[j+1]) {                inttemp =Arr[j]; ARR[J]= Arr[j+1]; Arr[j+1] =temp; }        }    }}

Sort-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.