# 1. Brief introduction of bubble sorting

Bubble Sort, also known as Bubble Sort, is a simple sorting algorithm. It repeatedly visits the columns to be sorted, compares two elements at a time, and exchanges them if they are in the wrong order. The work of visiting a number of columns is repeated until no exchanges are needed, that is, the columns have been sorted. The name of this algorithm comes from the fact that smaller elements "float" slowly to the top of a number of columns through an exchange.

The steps are as follows:

- Compare adjacent elements. If the first is larger than the second, swap the two.
- Do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. When this is done, the last element will be the maximum number
- Repeat the above steps for all elements except the last one.
- Continue repeating the above steps for fewer and fewer elements each time until no pair of numbers need to be compared.

Take an example of a one-dimensional array:

Five elements, 6, 9, 1, 2, 7, sort from smallest to largest

First round sorting:

- Current order: 691 127
- First we start with the first element on the left, and the first element compares with the second element, which is 6 and 9. Obviously 6 is smaller than 9 and does not exchange positions.
- The second element is then compared with the third element, 9 and 1, because 9 is larger than 1, the positions of both elements need to be exchanged
- After swapping, the order changes to: 6 1 9 2 7
- Then the third and fourth elements compare, that is, 9 and 2, obviously 9 is larger than 2, so swap their positions
- After swapping, the order changes to: 6 1 2 9 7
- Comparing the fourth element with the fifth element, 9 and 7, it is clear that 9 is larger than 7, exchanging their positions
- After swapping, the order changes to: 6 1 2 7 9

After the first round of sorting, we found that the largest element has been found and placed on the far right, which is 9. Obviously, this is not enough, because all the elements are not sorted from smallest to largest yet, so we're going to do a second round of sorting.

Second round of sorting:

- After the first round of sorting, the current order is: 61 2 7 9
- The first element on the left is compared with the first element on the right, which is 6 and 1. Obviously 6 is larger than 1, exchanging positions
- The order after exchange is: 1 6 2 7 9
- Comparing the second element with the third element, 6 and 2, it is clear to swap their positions
- The order after exchange is: 1 2 6 7 9

We're lucky here that it takes only two rounds to arrange the order, but actually more rounds may be needed to arrange all the elements in a regular way.

Worst case:

- Assuming there are x elements, let's arrange X-1 rounds together
- First round x - 1 Comparison
- Second round x - 2 Comparisons
- Third round x - 3 Comparisons
- . . .
- Last round comparison 1 time

You can try a few more examples, try sorting on your own, and you will find the above rules.

The code is as follows:

package com.personal.test; public class BubSort { public static void main(String[] args) { //Define a one-dimensional array int[] arr = {2, 0, -3, 10,1}; //Bubble sort int temp; //For element exchange for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if(arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } //Walk through the array to see the sorted results System.out.println("=====Sorted results======"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }

# 2. Sorting two-dimensional arrays by bubble sort

### Method One

The elements of a two-dimensional array are iterated through one-dimensional arrays in turn, then one-dimensional arrays are sorted by bubble sort, and then the elements of a one-dimensional array are iterated back into the two-dimensional array.

### Method 2

- Sort each row of a two-dimensional array by bubble
- Compare the end element of a row with the head element of the next row, that is, the maximum element of a row and the minimum element of a row. If the maximum elements are larger than the minimum elements, their positions are exchanged.
- Repeat until the end and head elements no longer exchange

For instance:

3 | 7 | 1 |
---|---|---|

2 | 3 | 1 |

10 | 2 | 3 |

First round sorting:

Sort each row by bubble, with the following results:

1 | 3 | 7 |
---|---|---|

1 | 2 | 3 |

2 | 3 | 10 |

The end element of each row is compared with the head element of the next row. If the tail element is larger than the header element, the exchange results are as follows:

1 | 3 | 1 |
---|---|---|

7 | 2 | 2 |

3 | 3 | 10 |

Because exchange occurs, repeat the process until the end elements are no longer exchanged.

When no more tail elements are exchanged, the elements of a two-dimensional array are arranged in order

The code is as follows:

public class Test2 { public static void main(String[] args) { //Create a two-dimensional array int[][] arr = {{1, 10}, {2, 9}, {-1, 9, -8}, {11, 0}}; new ArrSort().sort(arr); //Print an array to see what happens for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } } } /** * This class completes the sorting of two-dimensional arrays */ class ArrSort { public void sort(int[][] arr) { do { int temp;//This variable is used to exchange array elements //Outermost loop that traverses each row of a two-dimensional array for (int i = 0; i < arr.length; i++) { //Bubble sort on one-dimensional array of each row for (int j = 0; j < arr[i].length - 1; j++) { for (int k = 0; k < arr[i].length - 1 - j; k++) { if (arr[i][k] > arr[i][k + 1]) { temp = arr[i][k]; arr[i][k] = arr[i][k + 1]; arr[i][k + 1] = temp; } } } } //Detects the last element (maximum) of each line and compares it to the first element (minimum) of the next line, and if it is not less than or equal, swaps it int count = 0;//Count whether there is a swap, if there is one, the do while loop will not stop for (int i = 0; i < arr.length - 1; i++) { if (arr[i][arr[i].length - 1] > arr[i + 1][0]) { temp = arr[i][arr[i].length - 1]; arr[i][arr[i].length - 1] = arr[i + 1][0]; arr[i + 1][0] = temp; count++; } } //No swap occurred, terminating cycle if (count == 0) { break; } } while (true); }