1. 程式人生 > >Java 中冒泡排序

Java 中冒泡排序

public [] code png string bsp static ring pub

package com.nf147.test;

public class sort {
    public static void main(String[] args) {

        int arr[] = {11, 1, 3, 10, 7, 5, 20, 34, 14, 16};


        System.out.println("排序前");

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        System.out.println(
"=================="); 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 tem=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tem; } } }
for (int num : arr) { System.out.print(num + " "); } } }

技術分享圖片

Java 中冒泡排序