The principle of bubble sort and Java code implementation _java

Source: Internet
Author: User

Overview

Bubble sort is a simple sort algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until the sequence has been sorted. The name of the algorithm comes from the fact that the smaller elements float slowly through the exchange to the beginning of the sequence.

To put it simply, it is:

The bubble sort is to sink the larger number to the back of the array (which can be understood below), and the smaller to float in front (above).

Visual interpretation diagram:

Steps

Compare the adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same work for each pair of adjacent elements, from the first pair to the end of the last couple. At this point, the final element should be the largest number.
Repeat the above steps for all elements except the last one.
Continue to repeat the previous steps for less and fewer elements until no pair of digits need to be compared.
Instance

Raw data:

3 5 2 6 2
First round

Compare 3 and 5,5 is greater than 3, do not need
to Exchange 3 5 2 6
to continue comparing 2 and 2,5 greater than 5, Exchange position 2 3 2 + 5
continue to compare 6 and 6,6 greater than 2, do not need to Exchange
5 5
3 2 5 Continue to compare 6 and 2,6 greater than 2, swap position
3 2 5 2 6 6
sink to the last, two 2 are all up (fore).

Second round

Compare 3 and 2, 3 is greater than 2, Exchange position 2 3 5 2 6
Compare 3 and 5, 5 is greater than 3, do not need to Exchange 2 3 5 2 6
compare 5 and 2, 5 is greater than 2, Exchange position
2 3 2 5 6
    no need to compare 5 and 6.

Third round

Compare 2 and 3, 3 is greater than 2, do not need to Exchange 2 3 2 5 6
Compare 3 and 2, 3 is greater than 2, Exchange position 2 2 3 5 6
do not need to compare

Fourth round

Compare 2 and 2 without swapping
2 2 3 5 6

End of four rounds

2 2 3 5 6

Code implementation (Java)

Package com.coder4j.main.arithmetic.sorting; public class Bubble {/** * bubble sort * * @param array * @return/public static int[] Sort (int[) array)
    {int temp; The first cycle indicates the number of rotations compared, such as the length element, comparing the number of rotations to length-1 times (no need to compare) for (int i = 0; i < array.length-1; i++) {SYSTEM.O
      Ut.println ("First" + (i + 1) + "round start");  Second-tier loops, each adjacent to the two comparisons, the number of times with the increase in the number of wheels decreased, each round to determine one of the largest, do not need to compare the largest for (int j = 0; J < array.length-1-I. J + +) {if (Array[j + 1] < array[j])
          {temp = Array[j];
          ARRAY[J] = array[j + 1];
        Array[j + 1] = temp;
        System.out.println ("First" + (i + 1) + "round, First" + (j + 1) + "second comparison:");
        for (int k:array) {System.out.print (k + "");
      } System.out.println ();
      } System.out.println ("Result:");
      for (int k:array) {System.out.print (k + "");
    } System.out.println ();
  } return array;
  public static void Main (string[] args) {  Int[] Array = {3, 5, 2, 6, 2};
    int[] sorted = sort (array);
    SYSTEM.OUT.PRINTLN ("final result");
    for (int i:sorted) {System.out.print (i + "");

 }
  }

}

Test output results:

1th round start
1th round, 1th time Comparison:
3 5 2 6 2 
1th round, 2nd times comparison: 3 2 5 6 2 
1th turn, 3rd times comparison: 3 2 5 6 2 
1th Turn, 4th comparison:
3 2 5 2 6<  c9/> Results:
3 2 5 2 6 
2nd round start
2nd round, 1th time Comparison: 2 3 5 2 6 
2nd turn, 2nd times comparison: 2 3 5 2 6 
2nd turn, 3rd comparison:
2 3 2 5 6 
Results:
2 3 2 5 6 
3rd round start
3rd round, 1th time Comparison: 2 3 2 5 6 
3rd turn, 2nd comparison: 2 2 3 5 6 
results:
2 2 3 5 6 
4th round start
4th round, 1th time comparison:
2 2 3 5 6 
results:
2 2 3 5 6 
final result
2 2 3 5 6 

Tested, consistent with the results in the 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.