Algorithm-01 bubble sort

Bubble sort method:

first step:

Starting from the first digit, compare the two adjacent digits. If the front is greater than the back, then change the front to the back. After the cycle comparison is completed, the last digit is the largest.

The second step:

Starting from the second digit, compare two adjacent numbers, but there is no need to compare the last digit, and so on.

The specific implementation code is as follows:

package Test.com.lxy;

public class BubbleSort {

	public static void main(String[] args) {
		//新建一个数组
		int[] number1= new int[]{12,2,5,35,6,13,14,1};
		//获取数组长度
		int len=number1.length;
		//从数组第一位开始,当i小于数组长度时,执行操作后继续向后移1位
		for(int i=0;i<len;i++) {
			for(int j=0;j<len-i-1;j++) {
				//判断前一位数字是否大于前面一位数字,
				if(number1[j]>number1[j+1]) {
					//将后面一位数字与前面一位数字进行交换
					int temp=number1[j];
					number1[j]=number1[j+1];
					number1[j+1]=temp;
				}
			}
		}
		//打印出最后排序结果
		for(int i=0;i<len;i++) {
			System.out.println(number1[i]+"");
		}
	}

}

 

Guess you like

Origin blog.csdn.net/dream_18/article/details/115183331