package com.larry.sorting;
import java.util.Arrays;
/**
* 直接插入排序(从小到大)
* @author admin
*
*/
public class DirectInsertionSort {
private int[] directInsertionSort(int array[]){
int aLen = array.length;
if(aLen == 1 || aLen == 0) return array;
//直接插入排序核心算法
for(int i = 2; i <= array.length-1; i++){
int sym = array[i];
for(int j = i - 1; j >=0; j--){
if(sym < array[j]) array[j+1] = array[j];
else{
array[j+1] = sym;
break;
}
}
}
return array;
}
public static void main(String[] args) {
//int array[] = {1};
int array[] = {1, 2, 3, 7, 5, 5, 11};
System.out.println(Arrays.toString(new DirectInsertionSort().
directInsertionSort(array)));
}
}
插入排序算法-直接插入排序
最新推荐文章于 2019-07-30 22:34:26 发布