您的位置:首页 > 编程语言 > Java开发

直接插入排序(JAVA实现)

2017-04-23 15:42 375 查看
/**
* Created by DELL on 2017/4/23.
* 直接插入排序
*/
public class InsertSort {
public static void main(String[] args){
int[] in = {33,12,25,46,33,68,19,80};
int count = in.length;
int  j = 0;
InsertSortTest(in);
for(int i=0;i<count;i++){
System.out.print(in[i]+" ");
}
}
public static void InsertSortTest(int[] in){
int temp = 0;//O(1)的辅助空间
int j=0;
for(int i=1;i<in.length;i++){
if(in[i]<in[i-1]){
temp=in[i];
for( j=i-1;j>=0 && in[j]>temp;j--){
in[j+1] = in[j];
}
in[j+1] = temp;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: