Bubble ordering of Algorithms (Java example)

Source: Internet
Author: User

Bubble Sort ( English:Bubble sort)

is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

Animated schematic

implementation Examples

Java

1  Public classBubblesortexample {2     3     Static voidBubblesort (int[] arr) {4         intLen =arr.length;5         inttemp = 0;6          for(inti=0;i<len;i++){7 /*from 1 to Len-i8 for (int j=1;j< (len-i); j + +) {*/9 /*from len-1 to I*/Ten              for(intJ= (len-1); j>i;j--){ One                 if(arr[j-1]>Arr[j]) { A                     //Interchange Element -Temp=arr[j-1]; -arr[j-1]=Arr[j]; thearr[j]=temp; -                 } -             } -         } +     } -  +      Public Static voidMain (string[] args) { A         //TODO auto-generated Method Stub at         intarr[]={11,3,2,5,13,7,19}; -          -SYSTEM.OUT.PRINTLN ("Sequence before sorting:"); -          for(inti=0;i<arr.length;i++){ -System.out.print (arr[i]+ ""); -         } in System.out.println (); -          to Bubblesort (arr); +System.out.println ("Sorted sequence:"); -          for(inti=0;i<arr.length;i++){ theSystem.out.print (arr[i]+ ""); *         } $     }Panax Notoginseng  -}

Reference Links:

Bubble sort-Wikipedia

Bubble Sort in Java

Bubble ordering of Algorithms (Java example)

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.