73 lines
1.8 KiB
C++
73 lines
1.8 KiB
C++
#ifndef _QUICKSORT_HPP_
|
|
#error QSORT.HPP must precede QSORT.TPP
|
|
#endif
|
|
|
|
template <class T>
|
|
QuickSort<T>::QuickSort(void)
|
|
: mlpItemList(0)
|
|
{
|
|
}
|
|
|
|
template <class T>
|
|
QuickSort<T>::~QuickSort()
|
|
{
|
|
}
|
|
|
|
template <class T>
|
|
void QuickSort<T>::sortItems(T HUGE *lpItemList,long itemCount,SortOptions::SortOrder sortOrder)
|
|
{
|
|
mlpItemList=lpItemList;
|
|
if(SortOptions::Ascending==sortOrder)quickSort(0,itemCount);
|
|
else quickSortDescending(0,itemCount);
|
|
}
|
|
|
|
template <class T>
|
|
void QuickSort<T>::quickSort(long left,long right)
|
|
{
|
|
long tempLeft(left);
|
|
long tempRight(right);
|
|
T tempItem;
|
|
T swapItem;
|
|
|
|
tempItem=*((T FAR*)(mlpItemList+((left+right)/2L)));
|
|
do{
|
|
while(*(mlpItemList+tempLeft)<tempItem)tempLeft++;
|
|
while(tempItem<*((T FAR*)(mlpItemList+tempRight)))tempRight--;
|
|
if(tempLeft<=tempRight)
|
|
{
|
|
swapItem=*((T FAR*)(mlpItemList+tempLeft));
|
|
*((T FAR*)(mlpItemList+tempLeft))=*((T FAR*)(mlpItemList+tempRight));
|
|
*((T FAR*)(mlpItemList+tempRight))=swapItem;
|
|
tempLeft++;
|
|
tempRight--;
|
|
}
|
|
}while(tempLeft<=tempRight);
|
|
if(left<tempRight)quickSort(left,tempRight);
|
|
if(tempLeft<right)quickSort(tempLeft,right);
|
|
}
|
|
|
|
template <class T>
|
|
void QuickSort<T>::quickSortDescending(long left,long right)
|
|
{
|
|
long tempLeft(left);
|
|
long tempRight(right);
|
|
T tempItem;
|
|
T swapItem;
|
|
|
|
tempItem=*((T FAR*)(mlpItemList+((left+right)/2L)));
|
|
do{
|
|
while(*(mlpItemList+tempLeft)>tempItem)tempLeft++;
|
|
while(tempItem>*((T FAR*)(mlpItemList+tempRight)))tempRight--;
|
|
if(tempLeft<=tempRight)
|
|
{
|
|
swapItem=*((T FAR*)(mlpItemList+tempLeft));
|
|
*((T FAR*)(mlpItemList+tempLeft))=*((T FAR*)(mlpItemList+tempRight));
|
|
*((T FAR*)(mlpItemList+tempRight))=swapItem;
|
|
tempLeft++;
|
|
tempRight--;
|
|
}
|
|
}while(tempLeft<=tempRight);
|
|
if(left<tempRight)quickSortDescending(left,tempRight);
|
|
if(tempLeft<right)quickSortDescending(tempLeft,right);
|
|
}
|