Java realizes bubble sort + graphical bubble sort + code implementation + code analysis (java)

basic introduction

The basic idea of ​​Bubble Sorting is: by treating the sorting sequence from front to back (starting from the element with the smaller subscript), comparing
the values ​​of adjacent elements in turn , and swapping if the reverse order is found to make the element with the larger value Gradually move from the front to the back, and gradually rise upward like bubbles under the water

Insert picture description here
Since the above chestnuts are not very good, I found a new chestnut in the code analysis section (the code here represents the sorting process)

package data_structure;

import java.lang.reflect.Array;
import java.util.*;

public class BublleSortTest {
    
    
public static void main(String[] args) {
    
    
	int arr[]={
    
    3,9,-1,19,-2};
	//第一趟排序就是将最大的数排在最后
	int temp=0;//定义临时变量
	for(int i=0;i<arr.length-1;i++) {
    
    			
			if(arr[i] > arr[i+1]) {
    
    
				temp=arr[i];
				arr[i]=arr[i+1];
				arr[i+1]=temp;
			}
		}
	System.out.println("第一次排序后的数组");
	System.out.println(Arrays.toString(arr));
	
	//第二趟排序就是将第二大的数排在倒数第二位
		for(int i=0;i<arr.length-1-1;i++) {
    
    			
				if(arr[i] > arr[i+1]) {
    
    
					temp=arr[i];
					arr[i]=arr[i+1];
					arr[i+1]=temp;
				}
			}
		System.out.println("第二次次排序后的数组");
		System.out.println(Arrays.toString(arr));
		
		
	//第三趟排序就是将第三大的数排在倒数第三位
				for(int i=0;i<arr.length-1-2;i++) {
    
    			
						if(arr[i] > arr[i+1]) {
    
    
							temp=arr[i];
							arr[i]=arr[i+1];
							arr[i+1]=temp;
						}
					}
				System.out.println("第三次排序后的数组");
				System.out.println(Arrays.toString(arr));
				
				//第四趟排序就是将第四大的数排在倒数第四位
				for(int i=0;i<arr.length-1-3;i++) {
    
    			
						if(arr[i] > arr[i+1]) {
    
    
							temp=arr[i];
							arr[i]=arr[i+1];
							arr[i+1]=temp;
						}
					}
				System.out.println("第四次次排序后的数组");
				System.out.println(Arrays.toString(arr));
	}
}

Insert picture description here
Insert picture description here
If you have to write so many codes for a simple sorting, then it's just like playing with the mop with your crotch upright. Next, we will optimize the code.

package data_structure;

import java.lang.reflect.Array;
import java.util.*;

public class BublleSortTest {
    
    
public static void main(String[] args) {
    
    
	int arr[]={
    
    3,9,-1,19,-2};
	//第一趟排序就是将最大的数排在最后
	int temp=0;//定义临时变量
	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;
			}
		}
	System.out.println("第"+i+"次排序后的数组");
	System.out.println(Arrays.toString(arr));
	}
}
}

Analysis:
Insert picture description here
Expansion:

If there is a sorted array such as {1,2,3,4,5}, if we write in the above way, it will greatly increase the time (for an array with a smaller order of magnitude), then we can re Optimize the code.

package data_structure;

import java.util.*;

public class bolle {
    
    
	public static void main(String[] args) {
    
    
		int arr[] = {
    
     3, 9, 10, 19, 20 };
		
//		System.out.println("排序前");
//		System.out.println(Arrays.toString(arr));
//		
		bublleSort(arr);
//		
//		System.out.println("排序后");S
//		System.out.println(Arrays.toString(arr));
	}
	public static void bublleSort(int[] arr) {
    
    
		int temp = 0;// 定义临时变量
		boolean flag = false;// 标识变量,表示是否进行过交换
		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]) {
    
    
					flag = true;// 标识变量为真,那么就执行这段代码
					temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
			System.out.println("第"+i+"次排序");
			System.out.println(Arrays.toString(arr));

			if (flag == false) {
    
    // 在一趟排序中,一次都没交换过
				break;
			} else {
    
    
				flag = false;// 重置flag,进行下一次轮询(判断,循环)
			}
		}
	}
	
}

Analysis:
Insert picture description here
Insert picture description here
For example, the above code is already sorted, when the pointer points to each element and finds that it is sorted, it will exit directly.

Guess you like

Origin blog.csdn.net/weixin_46457946/article/details/113059548