Java bubble sort of keyboard input array

description

Bubble sorting means that the numbers with large values ​​are like bubbles, spit them out first, and then compare the first number with the following numbers in turn. Simply write here: For
     example , 3, 2, 1, 4, and 5 are
in the first In a loop, first compare 3 with the next number. If it is larger than the next number, swap, otherwise continue to compare the next number. After the first time, the first largest number will be found and arranged to the end.
Insert picture description hereInsert picture description here

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);
	} 
	
}

Guess you like

Origin blog.csdn.net/A_Tu_daddy/article/details/103536690