package com.arrays_;
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSortCustom {
public static void main(String[] args) {
int[] arr= {1,-2, 0, 2, 32};
bubble01(arr);
System.out.println("排序结果");
System.out.println(Arrays.toString(arr));
System.out.println("=====================");
bubble02(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int i1 = (Integer)o1;
int i2 = (Integer)o2;
return i2 - i1;
}
});
System.out.println("定制排序结果");
System.out.println(Arrays.toString(arr));
}
//使用冒泡完成排序
public static void bubble01(int[] a){
int temp = 0;
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
//从小到大排序
if (a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
//结合冒泡加定制
public static void bubble02(int[] a, Comparator c){
int temp = 0;
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
//数组排序由c.compare(a[j],a[j+1]>0)返回的值决定
if (c.compare(a[j],a[j+1])>0){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
}
//接口编程+动态绑定+匿名内部类的综合使用
标签:arr,temp,int,练习,System,定制,排序,out
From: https://www.cnblogs.com/Q1u-ovo/p/17277288.html