Java Bubble sort

Source: Internet
Author: User

/*

Array sorting: Generally ascending, element, small to large arrangement

Bubble sort: Compare adjacent elements in an array
Rule: Compare size, Position swap

*/

1  Public classarraymethodtest_2{2      Public Static voidMain (string[] args) {3         int[] arr = {3,1,4,2,56,7,0}; 4 Bubblesort (arr);5     }6 /*7 define methods to implement bubble ordering of arrays8 return value: No9 Parameters: ArraysTen     */ One      Public Static voidBubblesort (int[] arr) { A          for(inti = 0; i < arr.length-1; i++){ -             //The comparison of each inner loop, starting with the 0 index, is decremented each time -              for(intj = 0; J < Arr.length-i-1; J + +){ the                 //the index of the comparison is J and J+1 -                 if(Arr[j] > arr[j+1]){ -                     inttemp =Arr[j]; -ARR[J] = arr[j+1]; +ARR[J+1] =temp; -                 } +             } A         } at}

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