数据结构之每日一练-02(冒泡排序)

直接上代码

package com.test;

/**
 * 冒泡排序
 *
 * @Package: com.test
 * @ClassName: BubbleSort
 * @author: zq
 * @since: 2020/6/6 10:36
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
public class BubbleSort {

    public static void main(String[] args) {

        int[] arr = {1, 2, 6, 4, 5};
        arr = bubbleSort(arr);
        print(arr);


    }

    private static int[] bubbleSort(int[] 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;

                }

            }
        }
        return arr;

    }

    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

}

 

你可能感兴趣的:(数据结构之每日一练,冒泡排序)