Java algorithm-bubble sort

Source: Internet
Author: User

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.

The simplest implementation method, two-layer for loop, the inner loop to determine whether the next two elements in reverse order, the words will be two elements exchange, the outer loop once, can be the remaining elements in the array of the smallest element "float" to the front, so called bubble sort.

As usual, let's take a simple example:

Initial state: [24, 19, 26, 39, 36, 7, 31, 29, 38, 23] Inner layer first trip: [(9th [23]<->8th [38], +, 7, (+),) inner layer second trip: [(8th [23]<->7th [29], +, 7,----]) inner layer third trip: [(7th [23]<->6th [31], +, 7, +--)]) inner layer fourth trip: [24, 19, 26, 39, 36, 7, 23, 31, 29, 38] (7, 23are in the right order, no need to exchange) inner layer fifth trip: [(5th [7]<->4th [36], +, 7, (+),]) inner layer sixth trip: [(4th [7]<->3rd [39], +, 7,----]) inner layer seventh trip: [(3rd [7]<->2nd [26], +, 7,----]) inner layer eighth trip: [[7] (2nd [7]<->1st [19], +,---]) inner layer nineth trip: [7, (+), (+), (1st) [7]<->0th [24]) ...

In fact, the bubble sort is similar to the choice of sorting, the same number of times, is n * (n + 1)/2, but the bubble sort in the selection of the minimum value of the process will be an additional exchange (bubble sort in order to find the next element in the sequence of the wrong will be exchanged, corresponding to the selection of the sort, It is only after the end of the inner loop comparison that the exchange is determined according to the circumstances, so in my opinion, the selection sort is an improved version of the bubbling sort.

Implementation code:

/*** Bubble Method Sort <br/> * <li> compare adjacent elements. If the first one is bigger than the second one, swap them both. </li> * <li> for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number. </li> * <li> Repeat the above steps for all elements except the last one. </li> * <li> continue to repeat the above steps for fewer elements at a time until there are no more than one pair of numbers to compare. </li> * *@paramnumbers * Array of integers that need to be sorted*/   Public Static voidBubblesort (int[] numbers) {       intTemp//Record interim intermediate values    intsize = Numbers.length;//Array Size     for(inti = 0; i < size-1; i++) {            for(intj = i + 1; J < size; J + +) {               if(Numbers[i] < numbers[j]) {//Exchange two number of positionstemp =Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; }           }       }   }

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