插入排序
/**
* 插入排序
* Created by xueping.you on 15-8-5.
*/
public class InsertSort {
private final static Logger logger = LoggerFactory.getLogger(InsertSort.class);
public static void insertSort(int []unSortArray){
for(int i=1; i<unSortArray.length; i++){
if(unSortArray[i-1]>unSortArray[i]){
int j=i;
int temp=unSortArray[i];
while (j>0 && unSortArray[j-1]>temp){
unSortArray[j]=unSortArray[j-1];
j--;
}
unSortArray[j]=temp;
}
}
}
public static void main(String []args){
int [] array = new int[]{12,10,2,45,31,56,1,9};
logger.info("before:{}", array);
insertSort(array);
logger.info("after:{}",array);
}
}
result:
19:29:10.340 [main] INFO com.qyou.data.arithmetic.InsertSort - before:[12, 10, 2, 45, 31, 56, 1, 9]
19:29:10.352 [main] INFO com.qyou.data.arithmetic.InsertSort - after:[1, 2, 9, 10, 12, 31, 45, 56]