冒泡排序

https://www.cnblogs.com/shen-hua/p/5422676.html

package com.example.demo;

import java.util.Arrays;

public class BubbleSort {
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) { // 外层控制排序的趟数
            for (int j = 0; j < arr.length - 1 - i; j++) { // 内层控制每一趟对比多少次
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[] {1,3,6,2,10,0,11};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

posted @ 2025-03-31 16:45  zoomingxu  阅读(15)  评论(0)    收藏  举报