各自努力,最高处见!加油!
冒泡排序
public class ArraysSortCustom {
public static void main(String[] args) {
int[] arr={1,2,56,-1,8,65,100};
System.out.println("排序前");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("==============");
System.out.println("排序后");
new ArraysSortCustom().bubble01(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
//冒泡排序
public static void bubble01(int[] arr){
int temp=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < (arr.length-1-i); j++) {
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}
冒泡加定制
public class ArraysSortCustom {
public static void main(String[] args) {
int[] arr={1,2,56,-1,8,65,100};
System.out.println("排序前");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
bubble02(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int i1=(Integer)o1;
int i2=(Integer)o2;
return i1-i2;//更改这里的返回值结果会截然相反
}
});
System.out.println("==============");
System.out.println("排序后");
new ArraysSortCustom().bubble01(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
//冒泡排序加定制
public static void bubble02(int[] arr, Comparator c){
int temp=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < (arr.length-1-i); j++) {
if(c.compare(arr[j],arr[j+1])>0){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}