Java's bubble sort

Source: Internet
Author: User

Bubble sort because of its simplicity, usually we learn a sort algorithm for all kinds of programming languages first choose to learn

1  Public classBubblesort {2      Public Static voidMain (string[] args) {3         //creates an array, which is a random sequence of elements.4         int[] array = {666, 8, 28, 66, 86, 68 };5         //create an object for the bubbling sort class6Bubblesort Sorter =NewBubblesort ();7         //call sort method to sort an array8 Sorter.sort (array);9     }Ten     /** One * Bubble Sort A * The array to sort -      */ -      Public voidSortint[] Array) { the          for(inti = 1; i < Array.Length; i++) { -             //compares two adjacent elements, and the larger number bubbles up backwards -              for(intj = 0; J < Array.length-i; J + +) { -                 if(Array[j] > array[j + 1]) { +                     inttemp = Array[j];//keep the first element value in a temporary variable -ARRAY[J] = array[j + 1];//Save the second element value to the first element cell +Array[j + 1] = temp;//keep the temporary variable, which is the first element, in the second element A                 } at             } -         } -Showarray (array);//output bubble-sorted array elements -     } -     //Show all elements of an array -      Public voidShowarray (int[] Array) { in          for(intI:array) {//The foreach format iterates through the array -System.out.print (">" + i);//output each array element value to         } + System.out.println (); -     } the}

Conv New, published in the blog Park

Reprint please indicate the source, welcome mail exchange:[email protected]. com

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