int(__fileDECL*comp)(constvoid*,constvoid*)
)
#endif/*__USE_CONTEXT*/
{
char*lo,*hi;/*endsofsub-arraycurrentlysorting*/
char*mid;/*pointstomiddleofsubarray*/
char*loguy,*higuy;/*travelingpointersforpartitionstep*/
size_tsize;/*sizeofthesub-array*/
char*lostk[STKSIZ],*histk[STKSIZ];
intstkptr;/*stackforsavingsub-arraytobeprocessed*/
/*validationsection*/
_VALIDATE_RETURN_VOID(base!=NULL||num==0,EINVAL);
_VALIDATE_RETURN_VOID(width0,EINVAL);
_VALIDATE_RETURN_VOID(comp!=NULL,EINVAL);
if(num2)
return;/*nothingtodo*/
stkptr=0;/*initializestack*/
lo=(char*)base;
hi=(char*)base+width*(num-1);/*initializelimits*/
/*thisentrypointisforpseudo-recursioncalling:setting
loandhiandjumpingtohereislikerecursion,butstkptris
preserved,localsaren't,sowepreservestuffonthestack*/
recurse:
size=(hi-lo)/width+1;/*numberofel'stosort*/
/*belowacertainsize,itisfastertouseaO(n^2)sortingmethod*/
if(size=CUTOFF){
__SHORTSORT(lo,hi,width,comp,context);
}
else{
/*Firstwepickapartitioningelement.Theefficiencyofthe
algorithmdemandsthatwefindonethatisapproximatelythemedian
ofthevalues,butalsothatweselectonefast.Wechoosethe
medianofthefirst,middle,andlastelements,toavoidbad
performanceinthefaceofalreadysorteddata,ordatathatismade
upofmultiplesortedrunsappendedtogether.Testingshowsthata
median-of-threealgorithmprovidesbetterperformancethansimply
pickingthemiddleelementforthelattercase.*/
mid=lo+(size/2)*width;/*findmiddleelement*/
/*Sortthefirst,middle,lastelementsintoorder*/
if(__COMPARE(context,lo,mid)0){
swap(lo,mid,width);
if(__COMPARE(context,lo,hi)0){
swap(lo,hi,width);
if(__COMPARE(context,mid,hi)0){
swap(mid,hi,width);
/*Wenowwishtopartitionthearrayintothreepieces,oneconsisting
ofelements=partitionelement,oneofelementsequaltothe
partitionelement,andoneofelementsthanit.Thisisdone
below;commentsindicateconditionsestablishedateverystep.*/
loguy=lo;
higuy=hi;
/*Notethathiguydecreasesandloguyincreasesoneveryiteration,
soloopmustterminate.*/
for(;;){
/*lo=loguyhi,lohiguy=hi,
A[i]=A[mid]forlo=i=loguy,
A[i]A[mid]forhiguy=ihi,
A[hi]=A[mid]*/
/*Thedoubledloopistoavoidcallingcomp(mid,mid),sincesome
existingcomparisonfuncsdon'tworkwhenpassedthesame
valueforbothpointers.*/
if(midloguy){
do{
loguy+=width;
}while(loguymid__COMPARE(context,loguy,mid)=0);
if(mid=loguy){
}while(loguy=hi__COMPARE(context,loguy,mid)=0);
/*lologuy=hi+1,A[i]=A[mid]forlo=iloguy,
eitherloguyhiorA[loguy]A[mid]*/
higuy-=width;
}while(higuymid__COMPARE(context,higuy,mid)0);
/*lo=higuyhi,A[i]A[mid]forhiguyihi,
eitherhiguy==loorA[higuy]=A[mid]*/
if(higuyloguy)
break;
/*ifloguyhiorhiguy==lo,thenwewouldhaveexited,so
A[loguy]A[mid],A[higuy]=A[mid],
loguy=hi,higuylo*/
swap(loguy,higuy,width);
/*Ifthepartitionelementwasmoved,followit.Onlyneed
tocheckformid==higuy,sincebeforetheswap,
A[loguy]A[mid]impliesloguy!=mid.*/
if(mid==higuy)
mid=loguy;
/*A[loguy]=A[mid],A[higuy]A[mid];soconditionattop
ofloopisre-established*/
/*A[i]=A[mid]forlo=iloguy,
A[i]A[mid]forhiguyihi,
A[hi]=A[mid]
higuyloguy
implying:
higuy==loguy-1
orhiguy==hi-1,loguy==hi+1,A[hi]==A[mid]*/
/*Findadjacentelementsequaltothepartitionelement.The
doubledloopistoavoidcallingcomp(mid,mid),sincesome
existingcomparisonfuncsdon'tworkwhenpassedthesamevalue
forbothpointers.*/
higuy+=width;
if(midhiguy){
}while(higuymid__COMPARE(context,higuy,mid)==0);
if(mid=higuy){
}while(higuylo__COMPARE(context,higuy,mid)==0);
/*OK,nowwehavethefollowing:
lo=higuy=hi
A[i]=A[mid]forlo=i=higuy
A[i]==A[mid]forhiguyiloguy
A[i]A[mid]forloguy=ihi
/*We'vefinishedthepartition,nowwewanttosortthesubarrays
[lo,higuy]and[loguy,hi].
Wedothesmalleronefirsttominimizestackusage.
Weonlysortarraysoflength2ormore.*/
if(higuy-lo=hi-loguy){
if(lohiguy){
lostk[stkptr]=lo;
histk[stkptr]=higuy;
++stkptr;
}/*savebigrecursionforlater*/
if(loguyhi){
lo=loguy;
gotorecurse;/*dosmallrecursion*/
lostk[stkptr]=loguy;
histk[stkptr]=hi;
++stkptr;/*savebigrecursionforlater*/
hi=higuy;
/*Wehavesortedthearray,exceptforanypendingsortsonthestack.
Checkifthereareany,anddothem.*/
--stkptr;
if(stkptr=0){
lo=lostk[stkptr];
hi=histk[stkptr];
gotorecurse;/*popsubarrayfromstack*/
else
return;/*allsubarraysdone*/
有安装vs2008或2010吗,在安装目录下面的VC/src中自带有源代码。比如我的就在
D:\ProgramFiles\MicrosoftVisualStudio10.0\VC\crt\src中。没有的话发给你
1、首先标准只是规定了这些函数的接口和具体的运行效率的要求,这些函数具体是怎么写得要看各个编译器的实现和平台。
2、例如使用的编译器是visualstudio,微软提供了一部分C运行时(CRT)的源码,里面会有memcpy,strcpy之类的函数的实现,在visualstudio2005下的路径是C:\ProgramFiles\MicrosoftVisualStudio8\VC\crt\src。
C语言
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。