参考:https://www.cnblogs.com/onepixel/articles/7674659.html
冒泡排序
/**
* 冒泡排序
* @param arr
*/
public static void sorts(int [] arr){
for (int i = 0; i <arr.length; i++) {
int a;
for (int j = 0; j <arr.length-i-1 ; j++) {
if (arr[j]>arr[j+1]){
a=arr[j];
arr[j]=arr[j+1];
arr[j+1]=a;
}
}
}
}
选择排序
/**
* 选择排序
* @param arr
*/
public static void sort(int [] arr){
for (int i = 0; i <arr.length-1; i++) {
for (int j = i+1; j <arr.length ; j++) {
if (arr[i]>arr[j]){
int a=arr[i];
arr[i]=arr[j];
arr[j]=a;
}
}
}
}
测试
int[] a=new int[7];
a[0]=2131231231;
a[2]=44;
a[3]=23;
a[4]=112;
a[5]=123;
a[6]=11231;
a[1]=121312312;
int [] b={1,1232213,3,4,12,1223,213};
sorts(a);
System.out.println("a = " + Arrays.toString(a));
System.out.println("a = " + Arrays.toString(b));
sort(b);
System.out.println("a = " + Arrays.toString(b));