Java冒泡排序

it2022-05-09  24

public class Sort {  //public Sort(){}  public static void main(String args[]){   int[] num = new int[10] ;   int N = num.length ;   int temp = 0 ;      try{    for(int i=0 ; i<10 ; i++){     num[i] = Integer.parseInt(args[i]) ;    }       //排序开始    for(int i=0 ; i<N-1 ; i++){     for(int j=N-2 ; j>=i ; j--){      if(num[j]>num[j+1]){       temp = num[j] ;         num[j] = num[j+1] ;        num[j+1] = temp ;      }     }    }     //输出排序结果    for(int i=0; i<N; i++){     System.out.println(num[i]);    }   }catch(Exception ex){    //ex.printStackTrace() ;   }     }

}

编译:javac Sort.java

运行:java Sort 1 4 7 8 5 2 3 6 9 0

结果:0 1 2 3 4 5 6 7 8 9

转载于:https://www.cnblogs.com/nanshouyong326/archive/2009/03/23/1419793.html

相关资源:数据结构—成绩单生成器

最新回复(0)