import java.util.Arrays; public class Test1_冒泡排序 { public static void main(String[] args) { //Arrays.sort() 方法进行数组排序 int[] arr = {15, 36, 12, 14, 2, 6, 98, 26, 11}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); System.out.println("-------------------------------------"); //冒泡排序(升序) int[] arr1 = {15, 36, 12, 14, 2, 6, 98, 26, 11}; for (int i = 0; i < arr1.length - 1; i++) { for (int j = 0; j < arr1.length - 1 - i; j++) { if (arr1[j] > arr1[j + 1]) { int temp = arr1[j]; arr1[j] = arr1[j + 1]; arr1[j + 1] = temp; } } } System.out.println(Arrays.toString(arr1)); System.out.println("-------------------------------------"); //冒泡排序(降序) int[] arr2 = {15, 36, 12, 14, 2, 6, 98, 26, 11}; for (int i = 0; i < arr1.length - 1; i++) { for (int j = 0; j < arr1.length - 1 - i; j++) { if (arr2[j] < arr2[j + 1]) { int temp = arr2[j]; arr2[j] = arr2[j + 1]; arr2[j + 1] = temp; } } } System.out.println(Arrays.toString(arr2)); } }
JavaEE阶段冒泡排序
最新推荐文章于 2024-11-10 15:38:25 发布