您的位置:首页 > 其它

直接插入排序算法

2015-07-16 11:02 253 查看
/**
* 直接插入排序算法
* @author Cinn
*
*/
public class insertSort {

/**
* @param args
*/
public static void main(String[] args) {
int[] array= {48,58,50,98,69,51,33,99,100};
InsertSort(array);
printArray(array);
}

/**
* 直接插入排序
*/
public static void InsertSort(int[] array){
//定义一个临时变量
int temp = 0;
for(int i = 0 ; i<array.length;i++){
//j为前一个元素索引,i为当前元素索引
int j = i-1;
temp = array[i]; //当前元素的值
for(;j>=0&&temp<array[j];j--){
array[j+1] = array[j];
}
array[j+1] = temp;//每次相比提前减-1了,所以要加回来
}
}

public static void printArray(int[] array){
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  public