写这个算法的初衷是利用二分法排序为原型,借鉴C++的泛型的概念,写一个适合不同数据结构,适用于数组和指针的插入和排序一体的程序,但是时间和经理有限,经验也差很多,最近又有其他工作,只好把已经完成的部分贴出来,希望大家能够指正和完善,多多提出宝贵建议,谢谢。
//插入排序函数,v0.0
#include "stdafx.h"
//定义不同的数据结构,用户也可以自己定义
typedef int DataType;
//比较函数
int CompareDate(DataType Src, DataType Des)
{
if(Src>Des)
return 1;
else if(Src<Des)
return -1;
else
return 0;
}
//排序函数
bool CenFind(DataType TempNode, DataType *tHead, long &from , long &to,long &mid,int(*fn)(DataType ,DataType))
{
int i = 0;
DataType tCen = NULL;
mid = (from + to)/2;
tCen = tHead[mid];
i = fn(TempNode ,tCen );
if(i>0)
{
from = mid + 1;
if( (mid == from)||(mid == to))
return false;
CenFind(TempNode,tHead,from,to,mid,fn);
}
else if(i == 0)
{
return true;
}
else
{
to = mid - 1;
if( (mid == from)||(mid == to))
return false;
CenFind(TempNode,tHead,from,to,mid,fn);
}
}
int main(int argc, char* argv[])
{
DataType WholeDate[10] ;
DataType *tHead = NULL;
tHead = &WholeDate[0];
int i;
for(i = 0;i<10;i++)
{
WholeDate[i]=-1;
}
DataType Temp;
long from,to,mid;
long length=0;
for(i = 0;i<8;i++)
{
from = 0;
to = length;
mid = 0;
printf("DataType=");
scanf("%d",&Temp);
if(length==0)
{
WholeDate[length] = Temp;
length++;
}
else
{
if(CenFind(Temp, tHead, from , to,mid,CompareDate))
{
}
else
{
if(mid == length)
{
//add
/*for(int t = length; t>=to; t--)
{
WholeDate[t+1] = WholeDate[t];
}
WholeDate[to] = Temp;*/
WholeDate[length] = Temp;
}
else
{
//insert
for(int t = length; t>=to+1; t--)
{
WholeDate[t+1] = WholeDate[t];
}
WholeDate[to+1] = Temp;
}
length++;
}
}
}
for(i = 0;i<10;i++)
{
printf("WholeDate[%d] = %d\n",i,WholeDate[i]);
}
printf("Hello World!\n");
return 0;
}
本文地址:http://com.8s8s.com/it/it26758.htm