在学java泛型,于是把排序拿来练练手了
import java.util.Arrays; public class GenericArraySort { public static void main(String[] args){ Integer[] integers = getGenericArray(2,41,12,3); bubbleSort(integers); System.out.println("integer:"); System.out.println(Arrays.toString(integers)); Long[] longs = getGenericArray(123L, 22L,12L, 3L); selectSort(longs); System.out.println("long:"); System.out.println(Arrays.toString(longs)); Float[] floats = getGenericArray(3.2f,2.4f,1.5f, 22.2f); insertSort(floats); System.out.println("float:"); System.out.println(Arrays.toString(floats)); Double[] doubles = getGenericArray(5.2,2.4,1.5,22.2); mergeSort(doubles); System.out.println("double:"); System.out.println(Arrays.toString(doubles)); } /** * 比较2个数字的大小 * @param a * @param b * @return */ public static boolean compareNumberVal(Number a, Number b){ boolean ret = false; if(a instanceof Integer){ if(a.intValue() > b.intValue()){ ret = true; } }else if(a instanceof Long){ if(a.longValue() > b.longValue()){ ret = true; } }else if(a instanceof Float){ if(a.floatValue() > b.floatValue()){ ret = true; } }else if(a instanceof Double){ if(a.doubleValue() > b.doubleValue()){ ret = true; } }else if(a instanceof Byte){ if(a.byteValue() > b.byteValue()){ ret = true; } }else if(a instanceof Short){ if(a.shortValue() > b.shortValue()){ ret = true; } } return ret; } /** * 交换整型数组 i 和 j 的value * @param i * @param j * @param param * @param <T> */ public static <T extends Number> void exchangeKey(int i, int j, T param[]){ T tmp = param[i]; param[i] = param[j]; param[j] = tmp; } /** * 冒泡排序 * @param param * @param <T> */ public static <T extends Number> void bubbleSort(T param[]){ int len = param.length; for(int i = 1; i < len ; i++){ for(int j = len - 1; j >= i; j--){ //一一对比, 逐个替换 if(compareNumberVal(param[j - 1], param[j])){ exchangeKey(j - 1, j, param); } } } } /** * 快速选择排序 * @param param * @param <T> */ public static <T extends Number> void selectSort(T param[]){ int len = param.length; for(int i = 0; i < len ; i++){ T min = param[i]; int minIndex = i; int oi = i; for(int j = len - 1; j >= i; j--){ //一一对比, 取得最小的key 再替换 if(compareNumberVal(min, param[j])){ min = param[j]; minIndex = j; } } if(minIndex != oi){ exchangeKey(i, minIndex, param); } } } /** * 插入排序 * @param param * @param <T> */ public static <T extends Number> void insertSort(T param[]){ int len = param.length; for(int i = 1; i < len ; i++){ int j = i; T tmp = param[j]; //存临时变量 //逐个向后比较 假如大于 则往后移 while(j > 0 && compareNumberVal(param[j - 1], tmp)){ param[j] = param[j - 1]; j--; } param[j] = tmp; } } public static <T extends Number> void mergeSort(T param[]){ mergeProcess(param, 0, param.length - 1); } /** * 递归处理归并排序 * @param param * @param l * @param r * @param <T> */ public static <T extends Number> void mergeProcess(T param[], int l, int r){ if(l >= r){ return; } int mid = (l + r) / 2; //中间位置 把数组切分为2段 mergeProcess(param, l , mid ); mergeProcess(param, mid + 1 , r ); merge(param, l , r); } /** * 合并排序好的数组段 * @param param * @param l * @param r * @param <T> */ public static <T extends Number> void merge(T param[], int l, int r){ int mid = (l + r) / 2; //中间位置 把数组切分为2段 int rs = mid + 1; //右侧数组起始key 左 0 - mid 右 rs - r int cs = l; //临时排序数组的起始key(和原数组 l相同) int s = l; //备份原数组起始key Number[] tmpArr = new Number[param.length]; //临时排序数组 //重新排序 2个数组取最前面key 比较大小 放到 临时数组 while(l <= mid && rs <= r){ if(compareNumberVal(param[l], param[rs])){ tmpArr[cs++] = param[rs++]; }else{ tmpArr[cs++] = param[l++]; } } //左侧 剩余部分 拷贝 临时数组 while (l <= mid){ tmpArr[cs++] = param[l++]; } //右侧 剩余部分拷贝临时数组 while (rs <= r){ tmpArr[cs++] = param[rs++]; } //临时排序好的数组重新拷贝回 param while(s <= r){ param[s] = (T)tmpArr[s++]; } } /** * 通过参数获取泛型数组 * @param arg * @param <T> * @return */ public static <T> T[] getGenericArray(T ... arg){ return arg; } /** * 打印泛型数组 * @param a * @param <T> */ public static <T> void showArr(T a[]){ int len = a.length; for (int i = 0; i < len; i++){ System.out.println(a[i]); } } }