Bubble sort principle and Java code implementation

  • 2020-04-01 04:39:17
  • OfStack

An overview of the

Bubble sort is a simple sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The task of visiting the sequence is repeated until the sequence is sorted. The algorithm got its name because smaller elements float slowly to the beginning of the sequence by swapping.

To put it simply:

Bubble sort is when the larger and larger words of a bubble are dropped at the end of the array (understood as below) and the smaller ones float at the front (above).

Intuitive definition:

steps

Compare adjacent elements. If the first one is bigger than the second, you swap them.
Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the last element should be the largest number.
Repeat the above steps for all elements except the last one.
Keep repeating the above steps for fewer and fewer elements at a time until there are no pairs of Numbers to compare.
The instance

Original data:

Three, five, two, six, two
The first round


 To compare  3  and  5 . 5  Is greater than  3  There is no exchange 
3 5 2 6 2
 Continue to compare  5  and  2 . 5  Is greater than  2 , switch places 
3 2 5 6 2
 Continue to compare  5  and  6 . 6  Is greater than  5 There is no exchange 
3 2 5 6 2
 Continue to compare  6  and  2 . 6  Is greater than  2 , switch places 
3 2 5 2 6
6  Down to the end, two 2 Both come up (forward) respectively. 

The second round


 To compare  3  and  2 .  3  Is greater than  2 , switch places 
2 3 5 2 6
 To compare  3  and  5 .  5  Is greater than  3 There is no exchange 
2 3 5 2 6
 To compare  5  and  2 .  5  Is greater than  2 , switch places 
2 3 2 5 6
 Do not need to compare  5  and  6

In the third round


 To compare  2  and  3 .  3  Is greater than  2 There is no exchange 
2 3 2 5 6
 To compare  3  and  2 .  3  Is greater than  2 , switch places 
2 2 3 5 6
 No comparison 

The fourth round of


 To compare  2  and  2 There is no exchange 
2 2 3 5 6

End of the round

2, 2, 3, 5, 6

Code implementation (Java)


package com.coder4j.main.arithmetic.sorting;

public class Bubble {

  
  public static int[] sort(int[] array) {
    int temp;
    //The first loop indicates the number of rounds of comparison, such as length elements, and the number of rounds of comparison is length-1 (no need to compare with yourself).
    for (int i = 0; i < array.length - 1; i++) {
      System.out.println(" The first " + (i + 1) + " round ");
      //The second layer of the cycle, each adjacent two comparison, the number of times with the increase of the number of rounds continue to reduce, each round to determine a maximum, 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(" The first " + (i + 1) + " Wheel, the first " + (j + 1) + " Time is: ");
        for (int k : array) {
          System.out.print(k + " ");
        }
        System.out.println();
      }
      System.out.println(" Results: ");
      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(" The final result ");
    for (int i : sorted) {
      System.out.print(i + " ");
    }
  }

}

Test output results:


 The first 1 round 
 The first 1 Wheel, the first 1 Time is: 
3 5 2 6 2 
 The first 1 Wheel, the first 2 Time is: 
3 2 5 6 2 
 The first 1 Wheel, the first 3 Time is: 
3 2 5 6 2 
 The first 1 Wheel, the first 4 Time is: 
3 2 5 2 6 
 Results: 
3 2 5 2 6 
 The first 2 round 
 The first 2 Wheel, the first 1 Time is: 
2 3 5 2 6 
 The first 2 Wheel, the first 2 Time is: 
2 3 5 2 6 
 The first 2 Wheel, the first 3 Time is: 
2 3 2 5 6 
 Results: 
2 3 2 5 6 
 The first 3 round 
 The first 3 Wheel, the first 1 Time is: 
2 3 2 5 6 
 The first 3 Wheel, the first 2 Time is: 
2 2 3 5 6 
 Results: 
2 2 3 5 6 
 The first 4 round 
 The first 4 Wheel, the first 1 Time is: 
2 2 3 5 6 
 Results: 
2 2 3 5 6 
 The final result 
2 2 3 5 6 

The test results are consistent with those in the example.


Related articles: