1.sort排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Test { 2 public static void main(String[] args) { 3 4 int[] a = {1,6,9,3,2,8}; 5 for (int i = 0; i < a.length; i++) { 6 System.out.print(a[i]); 7 8 } 9 System.out.println(); 10 Arrays.sort(a); 11 12 for (int i = 0; i < a.length; i++) { 13 System.out.print(a[i]); 14 15 } 16 } 17 }
运行结果
2.冒泡排序法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Test { 2 public static void main(String[] args) { 3 4 int[] a = {1,6,9,3,2,8}; 5 for (int i = 0; i < a.length; i++) { 6 System.out.print(a[i]); 7 8 } 9 System.out.println(); 10 for (int i = 0; i < a.length; i++) { 11 for (int j = 0; j < a.length-1; j++) { 12 13 if (a[j]>a[j+1]) { 14 int temp=a[j]; 15 a[j]=a[j+1]; 16 a[j+1]=temp; 17 18 } 19 } 20 21 } 22 23 for (int i = 0; i < a.length; i++) { 24 System.out.print(a[i]); 25 26 } 27 } 28 }
运行结果