About the optimization of Java bubble sorting algorithm

The most basic bubble sort algorithm

// Java冒泡排序算法(最基础版本)
import java.util.Arrays;

//研究冒泡排序
public class BubbleSort {
    
    
	public static void main(String[] args) {
    
    
		//arrays自带的排序方法
		int[] a = {
    
    3,1,4,5,6};
		/*Arrays.sort(a);
		System.out.println(Arrays.toString(a));*/
		int inner = 0;
		int outner = 0;
		
		for (int j = 0; j < a.length; j++) {
    
    
			for (int i = 0; i < a.length-1; i++) {
    
    
				if (a[i]>a[i+1]) {
    
    
					int temp = a[i];
					a[i] = a[i+1];
					a[i+1] = temp;
				}
				System.out.println("当前内循环的次数:" + (++inner));
			}
			System.out.println("当前外循环的次数为:" + (++outner)+"当前数组的输出为:"+Arrays.toString(a));
		}
		
		System.out.println(Arrays.toString(a));
		
	}
}

The output of the algorithm is as follows:
Insert picture description here

This is the first bubble sorting algorithm written by most people (in order to show the quality of the optimization algorithm more intuitively below, directly record the number of inner and outer loops in the code), every future programmer who learns Java, You may all experience this most basic algorithm, and have you thought about making some optimizations for this algorithm?
The two optimizations of the algorithm I summarized below:

Optimization algorithm (1). About reducing the number of times to traverse the array each time

// 第一次优化
import java.util.Arrays;

//研究冒泡排序
public class BubbleSort {
    
    
	public static void main(String[] args) {
    
    
		//arrays自带的排序方法
		int[] a = {
    
    3,1,4,5,6};
		/*Arrays.sort(a);
		System.out.println(Arrays.toString(a));*/
		int inner = 0;
		int outner = 0;
		
		for (int j = 0; j < a.length-1; j++) {
    
    
			for (int i = 0; i < a.length-1-j; i++) {
    
    
				if (a[i]>a[i+1]) {
    
    
					int temp = a[i];
					a[i] = a[i+1];
					a[i+1] = temp;
				}
				System.out.println("当前内循环的次数:" + (++inner));
			}
			
			System.out.println("当前外循环的次数为:" + (++outner)+"当前数组的输出为:"+Arrays.toString(a));
		}
		
		System.out.println(Arrays.toString(a));
		
	}
}

The output after the first optimization is as follows:
Insert picture description here
The number of inner loops is reduced by half!!!
Through each inner loop, a maximum value is obtained and placed at the end of the array, and the second traversal directly skips this number. Get the first Optimization!

Use the opening and closing principle for early interrupt traversal (final version)

// 利用闭合原则对算法的二次优化
import java.util.Arrays;

//研究冒泡排序
public class BubbleSort {
    
    
	public static void main(String[] args) {
    
    
		//arrays自带的排序方法
		int[] a = {
    
    3,1,4,5,6};
		/*Arrays.sort(a);
		System.out.println(Arrays.toString(a));*/
		int inner = 0;
		int outner = 0;
		
		for (int j = 0; j < a.length-1; j++) {
    
    
			boolean flag = true;
			for (int i = 0; i < a.length-1-j; i++) {
    
    
				if (a[i]>a[i+1]) {
    
    
					flag = false;
					int temp = a[i];
					a[i] = a[i+1];
					a[i+1] = temp;
				}
				System.out.println("当前内循环的次数:" + (++inner));
			}
			
			System.out.println("当前外循环的次数为:" + (++outner)+"当前数组的输出为:"+Arrays.toString(a));
			if (flag) {
    
    
				System.out.println("提前就结束啦!");
				break;
			}
		}
		
		System.out.println(Arrays.toString(a));
		
	}
}

The output of the algorithm is as follows:
Insert picture description here
once again the number of traversals is reduced!!!
By defining a Boolean type variable flag in the outer loop, if the if statement of the inner loop is not executed, it means that no element is moved in the loop, that is, the array at this time The sorting has been completed, and there is no need to sort again.

Conclusion

When we first learned Java, it was not necessary to learn fast. After learning each knowledge point, we must dig deeper, and you may find other incredible things!!!

Guess you like

Origin blog.csdn.net/xiaole060901/article/details/107574678
Recommended