经典算法-冒泡排序

package array;

/**
 * @author 苗晓强
 * @date 2023/7/27 15:43
 */
public class BubbleSortTest {
    public static void main(String[] args) {
        int [] arrays = {2,4,8,5,6,3,7,9,0};

        for (int i = 0; i < arrays.length; i++){
            System.out.print(arrays[i] + "\t");
        }

        for (int i = 0; i < arrays.length - 1; i++){
            for (int j = 0; j < arrays.length - 1 - i; j++){
                if (arrays[j] > arrays[j+1]){
                    int temp = arrays[j];
                    arrays[j] = arrays[j+1];
                    arrays[j+1] = temp;
                }
            }
        }

        System.out.println();

        for (int i = 0; i < arrays.length; i++){
            System.out.print(arrays[i] + "\t");
        }
    }
}

你可能感兴趣的:(Java基础,Java基础)