/** * 冒泡排序 * @author fgtian * */ public class BubleSort { public static void sort(int[] arr) { int length = arr.length; for (int i = 0; i < length; i++) { for (int j = length - 1; j > i; j--) { int num1 = arr[j]; int num2 = arr[j - 1]; if (num2 > num1) { arr[j - 1] = num1; arr[j] = num2; } } } } public static void main(String[] args) { int len = 10; int[] arr = new int[len]; Random r = new Random(); for (int i = 0; i < len; i++) { arr[i] = r.nextInt(100) - 50; } sort(arr); for (int i = 0; i < len; i++) { System.out.print(" " + arr[i]); } } }
版权声明:本文博客原创文章,博客,未经同意,不得转载。