- public class DaoFactory1 {
- public static void mian(String[] args) throws Exception {
- int[] a={3,6,4,2,9,5,8,1,7,0};
- int[] b={1,6,4,2,9,5,8,1,7,0};
- int[] c={2,6,4,2,9,5,8,1,7,0};
- int[] d={5,6,4,3,5,5,8,1,7,0};
- System.out.println(a);//排序前
- a = bubbleSort(a);//调用排序方法
- System.out.println(a);//排序后
- }
- public static int[] bubbleSort(int[] arr) {//冒泡排序方法
- int temp = 0;
- int flag = 0;
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = 0; j < arr.length - i - 1; j++) {
- if (arr[j] < arr[j + 1]) {
- temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- flag = 1;
- }
- }
- if (flag == 0)
- break;
- }
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- return arr;
- }
- }
复制代码 把重复使用的代码重构成方法,以便重复使用. |