二分法插入排序

算法代码

/*
MODINARYSEARCH插入改良版
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include<time.h>
#define MAX 11
int MODBINARYSEARCH(int a[],int x,int n)//二分搜索 [logn]+1
{
	int	low=1,mid,high=n;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(x<a[mid])
			high=mid-1;
		else
			low=mid+1;
	}
	return low;	
}
void MODINARYSEARCH(int a[],int n)//
{
	int i,k,x;
	for(i=2;i<=n;i++)
	{
		x=a[i];
		k=MODBINARYSEARCH(a,x,i-1);
		
		for(int j=i-1;j>=k;j--)
			a[j+1]=a[j];
		a[k]=x;
	}
}
	
void main ()
{
	int i; 
	int a[MAX];
	memset(a,0x00,sizeof(int)*MAX);
	srand((unsigned)time(NULL));	
	i=1;
	while(i<MAX)
	{
		a[i] =rand()%100;
		printf("%d\t",a[i]);
		i++;
	}printf("\n");
	MODINARYSEARCH(a,MAX-1);
	i=1;
	while(i<MAX)
	{
		printf("%d\t",a[i]);
		i++;
	}
	
}

测试结果

二分法插入排序