简单算法之冒泡排序

package com.chasen;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {1, 45, 2, 57, 89};
        bubbleSort(arr);
        for (int i = 0; i < arr.length ; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    private static void bubbleSort(int[] arr) {
        int count = arr.length - 1;
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count - i; j++) {
                if (arr[j] >= arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }

        }
    }
}

猜你喜欢

转载自blog.csdn.net/ChasenZh/article/details/107724253