修改插入排序,将顺序查找改为二分查找

#include<iostream>
using namespace std ;
void InsertSort(int *a,int n){
	int r,l,m,temp;
	for(int i=1;i<n;i++){
		temp=a[i];
		l=0;
		r=i-1;
		while(l<=r){
			m=(l+r)>>1;
			if(a[m]<temp) l=m+1;
			else r=m-1;
		}
		for(int j=i-1;j>=l;j--)
			a[j+1]=a[j];
		a[l]=temp;
	}	
	for(int i=0;i<n;i++)
		cout << a[i] << " " ;
}
int main()
{
	int a[9]={9,3,2,5,4,1,7,4,14};
	InsertSort(a,9);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/h2017010687/article/details/81709958