对int类型的数据进行排序

public  class  Test {
     public  static  void  main(String[] args) {
         int [] a = { 1 8 5 2 4 9 };
         //冒泡排序
         for  ( int  k =  0 ; k < a.length -  1 ; k++) {
             for  ( int  j = k +  1 ; j < a.length; j++) {  // 升序把<改成>
                 if  (a[k] > a[j]) {
                     int  temp = a[k];
                     a[k] = a[j];
                     a[j] = temp;
                 }
             }
         }
         System.out.println( "排序后: " );
         for ( int  i =  0 ; i < a.length; i++){
             System.out.print(a[i] +  "  " );
         }
     }
}

猜你喜欢

转载自blog.csdn.net/u014304688/article/details/71189515