51,407
社区成员
发帖
与我相关
我的任务
分享
选择:每次循环取出最大的和第一个(循环的第一个,不是数组的第一个)交换
public static void xuanZe(int a[]){
int length = a.length;
for(int i=0;i<length-1;i++){
int max = i;
for(int j=i+1;j<length;j++){
if(a[i]<a[j]){
max = j;
}
}
if(max != i){
temp = a[i];
a[i] = a[max];
a[max] = temp;
}
}
}
冒泡
public static void maoPao(int a[]){
int length = a.length;
for(int i=0;i<length-1;i++)
for(int j=0;j<length-i-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
快速
public static void kuaiSu(int a[]){
int length = a.length;
for(int i=0;i<length-1;i++)
for(int j=i+1;j<length;j++){
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}