Algorithm (1)--java bubbling algorithm

Source: Internet
Author: User

The Java bubbling algorithm is one of the most common algorithms. So what is a bubbling algorithm? As shown (image from the network):

In fact, you can see that the first pass through the process, first compare the first pair of numbers, compared to the completion of the second number must be relatively large, then the second pair, the same comparison exchange, then this time the third number must be larger, so that all the number of comparison completed, The largest number bubbles to the far right of the array. In the second round you can put the second largest number in the second position on the right, and so on ... When you traverse a number that is not interchangeable, it means that the queue is already in order.

1  Public Static voidMain (string[] args) {2         int[] arr = {11, 13, 10, 34, 31, 24, 15, 16, 26, 9, 16, 4992, 356};3 Bubblesort (arr);//Sort complete, number is 9,10,11,13,15,16,16,24,26,31,34,356,4992,4     }5 6 7      Public Static voidBubblesort (int[] arr) {8          for(inti = 0; i < arr.length-1; i++) {9             BooleanFlag =true;//setting a token, if true, indicates that the sort has already been completed. Ten              for(intj = 0; J < arr.length-1-I; J + +) { One                 if(Arr[j] > arr[j + 1]) { A                     inttemp =Arr[j]; -ARR[J] = arr[j+1]; -ARR[J+1] =temp; theFlag =false; -                 } -             } -             if(flag) { +System.out.print ("Sort done, number is"); -                  for(intI1:arr) { +System.out.print (i1+ ","); A                 } at                  Break; -             } -         } -       -}

Algorithm (1)--java bubbling algorithm

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.