java冒泡排序-正序排列、倒序排列(逻辑思维)
- //本节课:1、数组冒泡排序:倒序、正序
- //1、冒泡正序:
- int num[] = {345, 2, 3, 777, 34, 233, 97, 30};
- for (int i = 0; i < num.length; i++) {
- for (int t = 0; t < num.length - 1 - i; t++) {
- if (num[t] > num[t + 1]) {
- int x = num[t];
- num[t] = num[t + 1];
- num[t + 1] = x;
-
- }
- }
- }
- System.out.println(Arrays.toString(num));
- //输出:[2, 3, 30, 34, 97, 233, 345, 777]
-
-
-
- //2、冒泡倒序:
- int num[] = {345, 2, 3, 777, 34, 233, 97, 30};
- for (int i = 0; i < num.length; i++) {
- for (int t = 0; t < num.length - 1 - i; t++) {
- if (num[t] < num[t + 1]) {
- int x = num[t];
- num[t] = num[t + 1];
- num[t + 1] = x;
-
- }
- }
- }
- System.out.println(Arrays.toString(num));
- //输出:[777, 345, 233, 97, 34, 30, 3, 2]
-
-
- //注意:
- //1、(num[t] > num[t + 1])为正序。(num[t] < num[t + 1])为倒序;
- //2、是加数字1不是i.
-
-
- //正序输出:
- String str = "";
- for (int i = 0; i < 7; i++) {
- str += "*";
- System.out.println(str);
- }
-
- /*
- //正序输出:
- *
- **
- ***
- ****
- *****
- ******
- *******
-
- */
-
-
- //倒序输出
- for (int i = 7; i >=1 ; i--) {
- String strs="";
- for(int t=0;t<i;t++){
- strs+="*";
- }
- System.out.println(strs);
- }
-
- /*
- 倒序输出:
- *******
- ******
- *****
- ****
- ***
- **
- *
- */