package com.iamitman.sort;
import java.util.Arrays;
/**
* 插入排序:
* 从第一个位置开始,找到满足条件的值,插入到之前排序好的对应位置
*
* @param <T>
*/
public class InsertSort<T extends Comparable> {
public T[] sort(T[] t) {
int length = t.length;
for (int i = 0; i < length - 1; i++) {
if (t[i + 1].compareTo(t[i]) < 0) {
T temp = t[i + 1];
int j = i;
while (j >= 0 && t[j].compareTo(temp) > 0) {
t[j + 1] = t[j];
j--;
}
t[j + 1] = temp;
}
}
return t;
}
public static void main(String[] args) {
InsertSort sort = new InsertSort();
Integer[] example = {6, 4, 2, 3, 5, 1, 7, 10, 8, 9};
System.out.println(Arrays.toString(sort.sort(example)));
}
}