stable_quick_sort.h File Reference


Detailed Description

This header files contains the defintion of the template function stable_quick_sort.

Definition in file stable_quick_sort.h.

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T, typename U>
void concatinate_data (U &lhs, U &pivot, U &rhs, U &output_data)
template<typename T, typename U>
void stable_quick_sort (const U &input_data, U &output_data)


Function Documentation

template<typename T, typename U>
void concatinate_data ( U &  lhs,
U &  pivot,
U &  rhs,
U &  output_data 
)

Assumes:
U is a container for objects of class T
U has a begin, and end function
U has an assignment operator<br/> Error Behavior:
if any of the assumptions do not hold, the code will not compile
Description:
This function concatinates the data in lhs, pivot, and rhs and stores the data in output_data

Definition at line 14 of file stable_quick_sort.h.

00015 {
00016   using namespace std;
00017 
00018   output_data = lhs;
00019   copy( pivot.begin(), pivot.end(), back_insert_iterator<U >( output_data));
00020   copy( rhs.begin(), rhs.end(), back_insert_iterator<U >( output_data));
00021 }

template<typename T, typename U>
void stable_quick_sort ( const U &  input_data,
U &  output_data 
)

Assumes:
U is a container for objects of class T
U has a begin, and end function
U has a random access iterator
U has a bidirctional iterator
U has a const forward iterator
U has a size function
U has a push_back, push_front and insert function
U has a copy constructor and a default constructor
U has an assignment operator<br/> U has a constructor which can copy a value an arbitrary number of times
Error Behavior:
if any of the assumptions do not hold, the code will not compile
Description:
This algorithm instantiates the quick sort algorithm. The data is sorted in a stable fasion
because the push_back, push_forwarad and insert functions are called at the appropriate places,
to preserve the order of equivalent elements.

Definition at line 41 of file stable_quick_sort.h.

00042 {
00043   using namespace std;
00044 
00045   if (input_data.size() <= 1) {
00046     output_data = input_data;
00047     return;
00048   } 
00049   int offset = input_data.size() / 2;
00050 
00051   U pivot( 1, input_data[offset]);
00052   U lhs, rhs;
00053   typename U::const_iterator pt;
00054   typename U::const_iterator pt_end;
00055 
00056   pt_end = input_data.begin() + offset;
00057   for (pt = input_data.begin();
00058        pt != pt_end;
00059        ++pt)
00060     {
00061       if (*pt < *pivot.begin()) {
00062         lhs.push_back( *pt);
00063       } else if (*pivot.begin() < *pt) {
00064         rhs.push_back( *pt);
00065       } else {
00066         if (pivot.size() == 1) {
00067           pivot.push_front( *pt);
00068         } else {
00069           pivot.insert( pivot.end()-1, *pt);
00070         }
00071       }
00072     }
00073 
00074   pt++ = pt_end;
00075   for (pt = input_data.begin(); 
00076        pt != input_data.end();
00077        ++pt)
00078     {
00079       if (*pt < *pivot.begin()) {
00080         lhs.push_back( *pt);
00081       } else if (*pivot.begin() < *pt) {
00082         rhs.push_back( *pt);
00083       } else {
00084         pivot.push_back( *pt);
00085       }
00086     }
00087 
00088   U lhs_sorted;
00089   stable_quick_sort< T, U>( lhs, lhs_sorted);
00090 
00091   U rhs_sorted;
00092   stable_quick_sort< T, U>( rhs, rhs_sorted);
00093 
00094   concatinate_data< T, U>( lhs_sorted, pivot, rhs_sorted, output_data);
00095 }


Generated on Fri Jan 7 12:36:19 2011 for public_options by  doxygen 1.5.1