案例:java中的基本排序
1 //冒泡排序 2 3 4 import java.util.Arrays; 5 6 7 public class ForTest{ 8 public static void main(String args[]){ 9 int[] arr = {34,54,12,45,23}; 10 System.out.println(Arrays.toString(arr)); 11 for(int i = 0; i <arr.length - 1; i++){ 12 for(int j = 0; j < arr.length - 1 -i; j++){ 13 if(arr[j]>arr[j+1]){ 14 int t = arr[j]; 15 arr[j] = arr[j+1]; 16 arr[j+1] = t; 17 } 18 } 19 } 20 System.out.print(Arrays.toString(arr));
21 }
22 }
//选择排序 import java.util.Arrays; public class ForTest{ public static void main(String args[]){ int[] arr ={23,345,654,12}; System.out.print(Arrays.toString(arr)); for(int i = 0; i <arr.length - 1; i++){ for(int j = i + 1; j <arr.length;j++){ if(arr[i]>arr[j]){ int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } } } System.out.println(Arrays.toString(arr)); } }