冒泡排序与选择排序
public static void method() { int[] arr = new int[10]; for (int i = 0; i < arr.length; i++) { arr[i] = (int) (Math.random() * 100 + 1); } for (int num : arr) { System.out.print(num + " "); } System.out.println("--------华丽的分割线--------"); for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { int tem; if (arr[j] > arr[j + 1]) { tem = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tem; } } System.out.println("这是第" + (i + 1) + "次排序结果:"); for (int num : arr) { System.out.print(num + " "); } System.out.println(); } System.out.println("最终排序结果为:"); for (int num : arr) { System.out.print(num + " "); } } public static void selectionsort() { int tem; int[] arr = new int[10]; for (int i = 0; i < arr.length; i++) { arr[i] = (int) (Math.random() * 100 + 1); } for (int num : arr) { System.out.print(num + " "); } System.out.println(); System.out.println("--------华丽的分割线--------"); for (int i = 0; i < arr.length - 1; i++) { int k = 0; for (int j = 1; j < arr.length - i; j++) { if (arr[k] > arr[j]) { k = j; } } System.out.println("第" + (i + 1) + "次," + "数组arr[]中的第" + (k + 1) + "个数最小,为" + arr[k]); tem = arr[arr.length - i - 1]; arr[arr.length - i - 1] = arr[k]; arr[k] = tem; System.out.println("第" + (i + 1) + "次排序结果为:"); for (int num : arr) { System.out.print(num + " "); } System.out.println(); } System.out.println("选择排序的最终结果为"); for (int num : arr) { System.out.print(num + " "); } } public static void main(String[] args) { method(); }
原文路径:http://www.cnblogs.com/sxx1993/p/7550025.html