用java写的常见排序
【摘要】 /** * @author Think *折半插入排序 */
public class binInsertSort {private static int a[]={22,3,43,11,99,88,34,34,33,22,11,1}; public static void main(String[] args) { i...
/**
* @author Think
*折半插入排序
*/
-
public class binInsertSort {
-
private static int a[]={22,3,43,11,99,88,34,34,33,22,11,1};
-
public static void main(String[] args) {
-
int []b=binInsertSort(a,0,12);
-
for(int i=0;i<b.length;i++){
-
System.out.print(a[i]+"\t");
-
}
-
}
-
public static int[] binInsertSort(int[] a,int low,int high){
-
for(int i=low+1;i<a.length;i++){
-
//保存插入元素
-
int temp=a[i];
-
//设置初始区间
-
int hi=i-1;
-
int lo=low;
-
//折半决定插入位置
-
while(lo<=hi){
-
int mid=(hi+lo)/2;
-
if(temp<a[mid]){
-
hi=mid-1;
-
}else{
-
lo=mid+1;
-
}
-
}
-
//移动在temp那个之后的元素
-
for(int j=i-1;j>hi;j--)
-
a[j+1]=a[j];
-
//插入元素temp
-
a[hi+1]=temp;
-
}
-
return a;
-
}
/**
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/47778299
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)