java二分插入排序

java二分插入排序

public class JWzw {
	public static void main(String[] args) {
		Integer[] in= {1,2,88 ,9,78,45,21,54,56,68,33,54,74,98};
		binaryInsertSort(in);
		}
	//二分插入排序
	public static void binaryInsertSort(Integer[] in) {
		for(int i=1;i<=in.length-1;i++) {
			if(in[i]<in[i-1]) {
				int temp=in[i];
				int low=0;
				int hight=i-1;
				while(low<=hight) {
					int mid=(low+hight)/2;
					if(temp<in[mid]) {
						hight=mid-1;
					}else {
						low=mid+1;
					}
				}
				for(int j=i-1;j>=low;j--) {
					in[j+1]=in[j];
				}
				in[low]=temp;
			}
		}
	}
}

你可能感兴趣的:(算法)