- package com.heima.test;
- import java.util.Arrays;
- public class Test10_冒泡反排序 {
- /*
- * 按照冒泡排序从小到大实现:9 8 7 6 5
- */
- public static void main(String[] args) {
- int[] arr = {6,7,9,5,8};
- getSortArr(arr);
- }
- public static void getSortArr(int[] arr) {
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = 0; j < arr.length - 1 - i; j++) {
- if (arr[j] < arr[j + 1]) {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- System.out.println(Arrays.toString(arr));
- }
- }
复制代码 |