public static void main(String[] args) { // 冒泡排序 int a[]=new int[10] ; int n=a.length; int t=0; Random random=new Random(); for (int i = 0; i < 10; i++) { a[i]=random.nextInt(20); System.out.print(a[i]+" "); } System.out.println("-----------"); while (n>=1){ for (int i = 0; i <n-1; i++) { if(a[i]>a[i+1]){ t=a[i]; a[i]=a[i+1]; a[i+1]=t; } } n--; } for (int c:a){ System.out.println(c); } System.out.println("-----------"); for (int i = 0; i <a.length-1 ; i++) { for (int j = 0; j <a.length-i-1 ; j++) { if(a[j]>a[j+1]){ t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for (int c:a){ System.out.println(c); } // 转化成字符串输出 System.out.println(Arrays.toString(a)); }
public static void main(String[] args) {
//选择排序
int a[]={2,32,12,11,65,5,0,88,46,10};
int t=0;
for (int i = 0; i <a.length-1 ; i++) {
for (int j = i+1; j <a.length-1 ; j++) {
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
System.out.println(Arrays.toString(a));
}