import org.junit.Test; public class maopaopaixu { public static void main(String[] args) { //从小到大 int arr[]={1,0,3,2,4}; 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; } } } for (int i = 0; i<arr.length; i++) { System.out.print(arr[i]+" "); } // 从大到小 int arr1[]={2,0,1,3,4}; 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; } } } for (int i = 0; i<arr1.length; i++) { System.out.print(arr1[i]+" "); } } }
冒泡排序简单案例(自己写的做个记录)
于 2021-12-25 22:54:23 首次发布