现在的位置: 首页 > 综合 > 正文

复习之冒泡排序

2018年05月26日 ⁄ 综合 ⁄ 共 623字 ⁄ 字号 评论关闭

继续上图和代码微笑

package com.lyj.sort.insert;

public class BubbleSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] array = { 49, 38, 65, 97, 76, 13, 27 };

        // 排序前
        System.out.print("排序前:     ");
        for (int i : array) {
            System.out.print(i + " ");
        }
        System.out.println();

        // 排序
        bubbleSort(array);

        // 排序后
        System.out.print("排序后:     ");
        for (int i : array) {
            System.out.print(i + " ");
        }
    }

    private static void bubbleSort(int[] array) {

        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
//            System.out.print("第 " + i + "次排序:  ");
//            for (int index : array) {
//                System.out.print(index + " ");
//            }
//            System.out.println();
        }
    }

}

抱歉!评论已关闭.