//冒泡排序;
int[] arr={2,4,1,3,5};
int temp=-1;
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println(Arrays.toString(arr));
//直接选择排序;
int[] arra={3,4,5,1,2};
for(int i=0;i<arra.length;i++) {
int tem = i;
for(int j=i;j<arra.length;j++) {
if(arra[j] < arra[tem]) {
tem = j;
}
}
int temp1 = arra[i];
arra[i] = arra[tem];
arra[tem] = temp1;
}
System.out.println(Arrays.toString(arra));
//反转排序;
int[] arrs={4,2,7,5,1};
for(int i=0;i<arrs.length / 2;i++) {
int temp2 = arrs[i];
arrs[i] = arrs[arrs.length-i-1];
arrs[arrs.length-i-1] = temp2;
}
System.out.println(Arrays.toString(arrs));
已经验证过了,都可以正确输出,大家可以放心借鉴。