Integer数组比较大小排序
话不多说直接上代码
public class InsertSort{
public static <T extends Comparable<? super T>> void a (T[] a){
for (int i = 0; i < a.length; i++) {
T tem = a[i];//保存当前位置i的元素,其中[0,i-1]已经有序
int j;
for (j = i; j>0 && tem.compareTo(a[j-1])<1; j--) {// 利用compareTo的比较结果来判断是大小(>=1是大->小,<1则是小->大)
a[j]=a[j-1];// 后移位置
}
a[j] = tem;// 插入到合适的位置
}
}
//for test purpose
public static void main(String[] args) {
Integer[] arr = {34,8,64,51,32,21,21,51};
a(arr);
for (Integer i : arr) {
System.out.print(i + " ");
}
}
}