java数组冒泡排序、转置(降序)
1.java 数组冒泡排序
排序的基本原理(升序):
  原始数据:  2 、1 、9 、0 、5 、3 、7 、6 、8;
第⼀次排序: 1  、2 、0 、5 、3 、7 、6 、8 、9 ;
第⼆次排序: 1  、0 、2 、3 、5 、6 、7 、8 、9 ;
第三次排序: 1 、 2 、3 、4 、5 、6 、7 、8 、9 ;
 以上是基础的原理过程,但是有⼀个问题,数据的不同可能排序的次数出现不同,但是有多少个数据,总的排序次数不会超过数组的长度。只要排序的次数达到长度*长度的次数,那么所有的数据就可以排序成功。
进⾏冒泡排序:
public class数组的排序 {
public static void main(String[] args) {
int data[] = new int[]{2,1,9,0,5,3,7,6,8};
get(data);
//外层控制排序的总次数
for (int y = 0 ; y < data.length ; y++){
//内层控制每次的排序控制
for (int x = 0 ; x <data.length-1 ; x++) {
if (data[x] > data[x+1]){
int t = data[x];
data[x] = data[x+1];
data[x+1] = t;
}
}
}
get(data);
}
//输出数据的⽅法
public static void get(int temp[] ){
for (int i = 0 ; i < temp.length ; i++) {
System.out.print(temp[i]+ "、");
}
System.out.println();
}
}
  改善设计:主⽅法设计上是作为程序的起点,既然是起点,所有的代码编写⼀定尽量简单,那么我们可以单独定义⽅法进⾏复杂操作。
1public class数组的排序 {
2public static void main(String[] args) {
3int data[] = new int[]{2,1,9,0,5,3,7,6,8};
4        get(data);
5        sort(data);
6        get(data);
7    }
8//输出数据的⽅法
9public static void get(int temp[] ){
10for (int i = 0 ; i < temp.length ; i++) {
11            System.out.print(temp[i]+ "、");
12        }
13        System.out.println();
14    }
15//负责排序的⽅法
16public static void sort(int s[]){
17//外层控制排序的总次数
18for (int y = 0 ; y < s.length ; y++){
19//内层控制每次的排序控制
20for (int x = 0 ; x <s.length-1 ; x++) {
21if (s[x] > s[x+1]){
22int t = s[x];
23                    s[x] = s[x+1];
24                    s[x+1] = t;
25                }
26            }
27        }
28    }
29 }
2.数组转置
  原始数据:  1 、 2 、3 、4 、5 、6 、7 、8  ;
转置后数据: 8 、 7 、6 、5 、4 、3 、2 、1  ;
要实现转置的操作有两个思路:
    定义⼀个新的数组,⽽后将原始数组按照排序的⽅式插⼊到新的数组之中,随后改变原始数组的引⽤;
1public class数组的排序 {
2public static void main(String[] args) {
3int data[] = new int[]{1,2,3,4,5,6,7,8};
4//⾸先定义⼀个新的数组,长度与原始数组⼀致
5int temp[] = new int[data.length];
6int foot = data.length -1 ;
7//对于新的数组按照索引由⼩到⼤的顺序循环
8for (int i = 0 ; i < temp.length ; i++) {
9            temp[i] = data[foot];
10            foot--;
11        }
12        data = temp; //data转向temp ,⽽data的原始数据就成为了垃圾
13        get(data);
14
15    }
16//输出数据的⽅法
17public static void get(int temp[] ){
18for (int i = 0 ; i < temp.length ; i++) {
19            System.out.print(temp[i]+ "、");
20        }
21        System.out.println();
22    }
23 }
    虽然上⾯的算法实现了转置的操作,但是代码⾥会产⽣垃圾 data的原始数据就是垃圾。
  利⽤算法,再⼀个数组上直接完成转置操作
  原始数据:  1 、 2 、3 、4 、5 、6 、7 、8  ;    //转换次数:数组长度 ÷ 2  记住不管数组是技术还是偶数转置次数⼀样
第⼀次转置: 8 、 2 、3 、4 、5 、6 、7 、1  ;
第⼆次转置: 8 、 7 、3 、4 、5 、6 、2 、1  ;
第三次转置: 8 、 7 、6 、4 、5 、3 、2 、1  ;
第四次转置: 8 、 7 、6 、5 、4 、3 、2 、1    ;
转置:
1public class数组的转置 {
2public static void main(String[] args) {
3int data[] = new int[]{1,2,3,4,5,6,7,8,9};
4        reverse(data);
5        get(data);
6    }
7//负责转置操作
8public static void reverse(int arr[]){
9int start = 0;
10int end = arr.length -1;
11for (int i = 0; i < arr.length/2 ; i++) {
12int temp = arr[start];
13            arr[start] = arr[end];
14            arr[end] = temp;
15            start ++;
16            end --;
17        }
18    }
19//此⽅法负责输出
20public static void get(int temp[]){
21for(int i = 0 ; i < temp.length ; i++) {
22            System.out.print(temp [i]+"、");
23        }
24        System.out.println();
25    }
26 }
>冒泡排序java代码详解