Bubble sort of sorting algorithm

Source: Internet
Author: User

Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science. 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 name of the algorithm is because the larger the element will slowly "float" to the top of the sequence, hence the name.
1  PackageEdu.aeon.algorithm;2 3 Importjava.util.Arrays;4 5 /**6 * Bubble Sort7  * @authorLzj8 * @create October 30, 2017 morning 9:43:209  */Ten  Public classBubblesort { One /** A * Bubble sorting algorithm implementation - * at October 30, 2017 a.m. 9:44:21 by Lzj - * @Parameters1 int[] Values to bubble sorted array the * @Returns void -  */ -      Public Static voidSortint[] values) { -         inttemp; +          for(inti=0;i<values.length;i++) { -              for(intj=0;j<values.length-1-i;j++) { +                 if(values[j]>values[j+1]) { A                     //swapping with third-party variables attemp =Values[j]; -Values[j]=values[j+1]; -values[j+1]=temp; -                 } -             } -         } in     } -     /** to * at October 30, 2017 a.m. 9:43:20 by Lzj + * @Parameters1 string[] args - * @Returns void the      */ *      Public Static voidMain (string[] args) { $         //TODO auto-generated Method StubPanax Notoginseng         int[] values=New int[10]; -         //Initializes a random integer with a length of 10 and an element of 1 to 10 the          for(inti=0;i<values.length;i++) { +Values[i]= (int) (Math.random () *10); A         } theSYSTEM.OUT.PRINTLN ("The result before sorting is:" +arrays.tostring (values)); + Bubblesort.sort (values); -SYSTEM.OUT.PRINTLN ("The result after sorting is:" +arrays.tostring (values)); $     } $  -}

The results of the execution are attached:

Bubble sort of sorting 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.