java 冒泡排序案例

package paixu;

public class maopao {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    54,33,99,1000000,44,87,88};
        System.out.println(arraytoS(arr));
        for (int k = 0;k<arr.length-1;k++){
    
    
            for (int i=0;i<arr.length-1-k;i++){
    
    
                if(arr[i]>arr[i+1]){
    
    
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                }
            }
        }

        System.out.println(arraytoS(arr));
    }
    public static String arraytoS(int [] arr){
    
    
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i=0;i<arr.length;i++){
    
    
            if(i==arr.length-1){
    
    
                sb.append(arr[i]);
            }else{
    
    
                sb.append(arr[i]).append(',');
            }
        }
        sb.append("]");
        String s = sb.toString();
        return s;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34608447/article/details/113621461