Time：2021-4-7

# brief introduction

Sorting is probably the most basic and commonly used of all algorithms. Sorting is a very classic problem. It reorders the items in an array (or a list) in a certain order.

There are many sorting algorithms, each of which has its own advantages and limitations.

Today we learn the simplest bubble sort algorithm.

# The principle of bubble sort

The principle of bubble sorting is very simple. Let’s imagine the process of bubble floating one by one.

Suppose we have eight numbers 29, 10, 14, 37, 20, 25, 44, 15 to sort.

Let’s first use an animation to intuitively observe the whole bubble sorting process:

There are eight rounds of sorting, each round will do pairwise comparison, and move the larger elements to the right, just like a bubble.

At the end of the round, the largest of the eight elements, 44, will move to the far right.

Then repeat the other rounds. Finally, we get a fully sorted array.

It can also be seen as follows:

The first round is to move the maximum 44 of the eight elements to the rightmost position.
The second round is to move the second largest value 37 of the eight elements to the rightmost position.
and so on.

# Java implementation of bubble sort algorithm

Let’s first look at the simplest bubble algorithm

``````public class BubbleSort {

public void doBubbleSort(int[] array){
log.info (the array before sorting is: {} ", array);
//Outer loop, traversing all rounds
for(int i=0; i< array.length-1; i++){
//Inner circulation, pairwise comparison, select the larger number, exchange
for(int j=0; j<array.length-1; j++){
if(array[j]>array[j+1]){
//Exchange two numbers
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
log.info (the array sorted in the {} round is: {} ", I + 1, array);
}
}

public static void main(String[] args) {
int[] array= {29,10,14,37,20,25,44,15};
BubbleSort bubbleSort=new BubbleSort();
bubbleSort.doBubbleSort(array);
}

}``````

This algorithm is two-layer traversal, the outer traversal is the number of rounds. Inner traversal represents the sort of each round.

Let’s look at the output:

# The first improvement of bubble algorithm

After analyzing the above traversal process, we can find that after the first sorting, 44 has been placed in the rightmost position and has been sorted.

After the second sorting, 37 has been sorted. After each round, the number of internal loop comparisons can be reduced by one.

This means that in the internal circulation, we only need to carry out the internal circulation array.length -I-1 comparison is OK.

The modification code is as follows:

``````public class BubbleSort1 {

public void doBubbleSort(int[] array){
log.info (the array before sorting is: {} ", array);
//Outer loop, traversing all rounds
for(int i=0; i< array.length-1; i++){
//Inner circulation, pairwise comparison, select the larger number, exchange, the last I number has been arranged in order, do not need to compare
for(int j=0; j<array.length-i-1; j++){
if(array[j]>array[j+1]){
//Exchange two numbers
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
log.info (the array sorted in the {} round is: {} ", I + 1, array);
}
}

public static void main(String[] args) {
int[] array= {29,10,14,37,20,25,44,15};
BubbleSort1 bubbleSort=new BubbleSort1();
bubbleSort.doBubbleSort(array);
}

}``````

Results of operation:

We can see that the running results are not different, but we have done a few less comparisons.

# The second improvement of bubble algorithm

From the above results, we can see that the sorting is actually completed after the fifth round. But we still do the sixth and seventh sorting.

Is there any way to judge whether the sorting has been completed?

Let’s think about it. In the inner loop, we do pairwise comparisons and then swap positions.

If there is no interaction during a certain traversal, it means that the sorting has been completed.

So we can introduce another flag to judge.

``````public class BubbleSort2 {

public void doBubbleSort(int[] array){
log.info (the array before sorting is: {} ", array);
//Outer loop, traversing all rounds
for(int i=0; i< array.length-1; i++){
//Add a flag. If there is no sorting in this round, it means that the sorting has ended and you can exit ahead of time
boolean flag=false;
//Inner circulation, pairwise comparison, select the larger number, exchange, the last I number has been arranged in order, do not need to compare
for(int j=0; j<array.length-i-1; j++){
if(array[j]>array[j+1]){
//Exchange two numbers
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
log.info (the array sorted in the {} round is: {} ", I + 1, array);
if(!flag)
{
log.info ("no sorting change in this round, sorting ends");
return;
}
}
}

public static void main(String[] args) {
int[] array= {29,10,14,37,20,25,44,15};
BubbleSort2 bubbleSort=new BubbleSort2();
bubbleSort.doBubbleSort(array);
}

}``````

The results are as follows

From the results, we can see that there is one less round of sorting, which improves the speed.

# Time complexity of bubble sort

Although we can do some performance optimization when bubbling, we basically need to do two iterations of nesting. The number of iterations is approximately = n * n, so the time complexity of bubble sorting algorithm is O (N 2).

learn-algorithm