JAVA排序(冒泡,直接选择,反转)

int[] arr = {45, 34, 53, 43};

一,直接选择排序

System.out.println(Arrays.toString(arr));
        for(int i=0;i<arr.length;i++) {
            int tem = i;
            for(int j=i;j<arr.length;j++) {
                if(arr[j] < arr[tem]) {
                    tem = j;
                }
            }
            int temp1 = arr[i];
            arr[i] = arr[tem];
            arr[tem] = temp1;
        }
        System.out.println(Arrays.toString(arr));

 

二,冒泡排序

System.out.println(Arrays.toString(arr));
        int temp = -1;
        for(int i=0;i<arr.length;i++) {
            for(int j=i+1;j<arr.length;j++) {
                if(arr[i]>arr[j]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));

 

三,反转排序

System.out.println(Arrays.toString(arr));
        for(int i=0;i<arr.length / 2;i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length-i-1];
            arr[arr.length-i-1] = temp;
        }
        System.out.println(Arrays.toString(arr));

 

posted @ 2017-06-10 16:51  for米洛  阅读(235)  评论(0编辑  收藏  举报