Friday, October 3, 2008

Quick Sort

#ifndef QUICKSORT
#define QUICKSORT

#include

namespace mystd
{
template
void Partition(T* sortee, int *pivotIndex,
int leftIncreaseIterator, 
int rightDecreaseIterator)
{
*pivotIndex = leftIncreaseIterator; 
if (leftIncreaseIterator 
<>
{
while(leftIncreaseIterator 
<>
{
while(*(sortee + leftIncreaseIterator)
< *(sortee + *pivotIndex))
{
++leftIncreaseIterator;
}

while(*(sortee + rightDecreaseIterator)
> *(sortee + *pivotIndex))
{
--rightDecreaseIterator;
}

if (leftIncreaseIterator 
<>
{
std::swap(*(sortee + leftIncreaseIterator), 
*(sortee + rightDecreaseIterator));
}
}
}
*pivotIndex = rightDecreaseIterator;
}

template  
void QuickSort(T* sortee, int left, int right)
{
int pivotIndex = 0;

if (left <>
{
Partition(sortee, &pivotIndex, left, right);
QuickSort(sortee, left, pivotIndex - 1);
QuickSort(sortee, pivotIndex + 1, right);
}
else
{
if (left <>
{
std::sort((sortee + left), (sortee + right));
}
}
}
}



/*
If size drop to below 20 the called
Shell Sort

*/






























#endif

No comments: