数组排序: 选择排序(直接选择排序):
public static void main(String[] args) {
int[]array={12,34,86,11,24};
for(int i=0;i<array.length;i++){
for (int j=0;j<array.length;j++){
if(array[i]>array[j]){
array[i]=array[i]+array[j];
array[j]=array[i]-array[j];
array[i]=array[i]-array[j];
}
}
}
System.out.println(Arrays.toString(array));
}
交换排序(冒泡排序):
int[] array = {12, 34, 86, 11, 24,1,2};
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length-i-1;j++){
if(array[j]>array[j+1]){
array[j]=array[j]+array[j+1];
array[j+1]=array[j]-array[j+1];
array[j]=array[j]-array[j+1];
}
}
}
System.out.println(Arrays.toString(array));
|