1 import java.util.Arrays; 2 3 public class Array04 { 4 public static void main(String[] args) { 5 //冒泡排序 6 int[] arr = {45,23,0,5,2,63,4,3,373,5,8,59,3}; 7 sort(arr); 8 System.out.println(Arrays.toString(arr)); 9 } 10 public static int[] sort(int[] arr){ 11 int temp = 0; 12 for (int i = 0; i < arr.length-1; i++) { 13 boolean flag= false; 14 for (int j = 0; j < arr.length-1; j++) { 15 if (arr[j]> arr[j+1]){ 16 temp= arr[j]; 17 arr[j]= arr[j+1]; 18 arr[j+1]= temp; 19 flag= true; 20 } 21 } 22 if (flag= false){ 23 break; 24 } 25 } 26 return arr; 27 } 28 }
结果
[0, 2, 3, 3, 4, 5, 5, 8, 23, 45, 59, 63, 373]
原文:https://www.cnblogs.com/zkn-bebut/p/15101089.html