Java对键盘输入的数组进行冒泡排序

描述

冒泡排序即 将值大的数像泡泡一样,先吐出来,依次将第一个数拿出来与后面的数进行比较,在这里简单写一下:
     如3,2,1,4,5
在第一次循环中,先将 3 与其后数进行比较,若比其后数大,则交换,否则继续比较后边的数,第一次之后会将第一个最大的数找到并排到最后。
在这里插入图片描述在这里插入图片描述

public class Solution {
    
    
	public static void sortIntegers(int[] A) {
    
    
		int temp = 0;
        for(int i = 0;i < A.length ; i++){
    
    
        	for( int j = 0;j < A.length - i - 1;j++){
    
    
        		if(A[j]>A[j + 1]){
    
    
        			temp = A[j];
        			A[j] = A[j+1];
        			A[j+1] = temp;
        		}
        	}
        }
    }
    
    public static void prnt(int a []){
    
    
		for(int j = 0;j<a.length;j++){
    
    
			System.out.print(a[j]+",");
		}
    }
    
	public static void main(String args[]){
    
    
		Scanner sc = new Scanner(System.in);
		String str = sc.next().toString();
		String[] arry = str.split(",");
		int[] b = new int[arry.length];
		for(int i = 0;i<b.length;i++){
    
    
			b[i] = Integer.parseInt(arry[i]);
		}
		sortIntegers(b);
		prnt(b);
	} 
	
}

猜你喜欢

转载自blog.csdn.net/A_Tu_daddy/article/details/103536690