Monday, April 13, 2009

My sorting method

I devised this quirky sorting method while in college.

It works by first partially sorting the array then passing the result to the standard built-in method. In this case the std::sort is quick sort.

template< typename Iterator>
void hybridSort(Iterator first, Iterator last)
{
Iterator j,k;
// do a single sort iteration
j = first;
for (k=last-1; k!=first; --k)
{
if (*j > *k)
{
std::iter_swap(j,k);

}

}
// pass the result to the standard sorting method
std::sort(first, last);

}


It's really not a significant improvement over just using quick sort. In cases where the built-in sort is inefficient, it does work better.

Here's sample output of test program I wrote. It uses randomly generated arrays for the tests.

Testing quickSort size = 256 n_times = 1000
CPU cycles 171
Testing bubbleSort size = 256 n_times = 1000
CPU cycles 1032
Testing hybridSort size = 256 n_times = 1000
CPU cycles 156

Testing hybridSort

27455 8899 13631 3296 27180 12651 23352 18534
small array unsorted

3296 8899 12651 13631 18534 23352 27180 27455
small array sorted

No comments:

Post a Comment