*** empty log message ***

This commit is contained in:
jiri
2006-04-01 12:58:57 +00:00
parent 1844f777ed
commit 1840ed3b34
5 changed files with 36 additions and 48 deletions

21
qsort.h
View File

@@ -43,18 +43,19 @@ return parity;
}
template<typename INDEX>
int genqsort2(INDEX l, INDEX r, bool (*bigger)(const INDEX, const INDEX), void (*swap)(const INDEX,const INDEX))
//for SORTABLE classes which provide LA_sort_traits<SORTABLE,INDEX,type>::compare and swap member functions
template<int type, typename SORTABLE, typename INDEX>
int memqsort(SORTABLE &object, INDEX l, INDEX r)
{
INDEX i,j,piv;
int parity=0;
if(r<=l) return parity; //1 element
if(bigger(l,r)) {parity^=1; swap(l,r);}
if(LA_sort_traits<SORTABLE,INDEX,type>::compare(object,l,r)) {parity^=1; object.swap(l,r);}
if(r-l==1) return parity; //2 elements and preparation for median
piv= (l+r)/2; //pivoting by median of 3 - safer
if(bigger(l,piv)) {parity^=1; swap(l,piv);} //and change the pivot element implicitly
if(bigger(piv,r)) {parity^=1; swap(r,piv);} //and change the pivot element implicitly
if(LA_sort_traits<SORTABLE,INDEX,type>::compare(object,l,piv)) {parity^=1; object.swap(l,piv);} //and change the pivot element implicitly
if(LA_sort_traits<SORTABLE,INDEX,type>::compare(object,piv,r)) {parity^=1; object.swap(r,piv);} //and change the pivot element implicitly
if(r-l==2) return parity; //in the case of 3 elements we are finished too
//general case , l-th r-th already processed
@@ -62,23 +63,23 @@ i=l+1; j=r-1;
do{
//important sharp inequality - stops at sentinel element for efficiency
// this is inefficient if all keys are equal - unnecessary n log n swaps are done, but we assume that it is atypical input
while(bigger(piv,i++));
while(LA_sort_traits<SORTABLE,INDEX,type>::compare(object,piv,i++));
i--;
while(bigger(j--,piv));
while(LA_sort_traits<SORTABLE,INDEX,type>::compare(object,j--,piv));
j++;
if(i<j)
{
// swap and keep track of position of pivoting element
parity^=1; swap(i,j);
parity^=1; object.swap(i,j);
if(i==piv) piv=j; else if(j==piv) piv=i;
}
if(i<=j) {i++; j--;}
}while(i<=j);
if(j-l < r-i) //because of the stack in bad case process first the shorter subarray
{if(l<j) parity ^=genqsort2(l,j,bigger,swap); if(i<r) parity ^=genqsort2(i,r,bigger,swap);}
{if(l<j) parity ^=memqsort<type,SORTABLE,INDEX>(object,l,j); if(i<r) parity ^=memqsort<type,SORTABLE,INDEX>(object,i,r);}
else
{if(i<r) parity ^=genqsort2(i,r,bigger,swap); if(l<j) parity ^=genqsort2(l,j,bigger,swap);}
{if(i<r) parity ^=memqsort<type,SORTABLE,INDEX>(object,i,r); if(l<j) parity ^=memqsort<type,SORTABLE,INDEX>(object,l,j);}
return parity;
}