您的位置:首页 > 其它

插入类------二分插入排序

2014-11-03 22:34 176 查看
package com.sksort;

public class insert_Binary {
public static void main(String[] args) {
int a[] = {5,5,4,5,2,1,8,0,5,9};

insert_Binary s = new insert_Binary();
s.sort(a);
s.show(a);
}

public void sort(int a[]) {
int i, j ,temp, low, high, mid = 0;

for (i = 1; i < a.length; i++)
if (a[i] < a[i-1]) {
temp = a[i];

low = 0;
high = i - 1;
while (low < high) {
mid = (low + high) / 2;
if(temp == a[mid]){
low = mid+1;
break;
}
else if (temp > a[mid])
low = mid+1 ;
else
high = mid-1 ;
}

for (j = i; j > low; j--)
a[j] = a[j-1];

a[low] = temp;
}
}

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