Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

QuickSort.h

Go to the documentation of this file.
00001 #ifndef Impala_Util_QuickSort_h
00002 #define Impala_Util_QuickSort_h
00003 
00004 
00005 /*****************************
00006  * quicksort algorithm copied from http://www.uow.edu.au/~nabg/ABC/quicks.html
00007  * code is edited to conform to coding standards
00008  *****************************/
00009 
00010 namespace Impala
00011 {
00012 namespace Util
00013 {
00015     const int cQuicksortTreshold = 15; 
00016 
00018 //    normal quicksort and selection sort        //
00020 
00022 template<class Type>
00023 void SelectionSort(Type data[], int left, int right)
00024 {
00025         for(int i=left ; i<right ; i++)
00026     {
00027                 int min = i;
00028                 for(int j=i+1 ; j<=right ; j++)
00029                         if(data[j] < data[min])
00030                 min = j;
00031                 Type temp = data[min];
00032                 data[min] = data[i];
00033                 data[i] = temp;
00034         }
00035 }
00036 
00038 template<class Type>
00039 int Partition(Type* data, int left, int right)
00040 {
00041         Type val = data[left];
00042         int lm = left-1;
00043         int rm = right+1;
00044         for(;;)
00045     {
00046                 do
00047                         rm--;
00048                 while (data[rm] > val);
00049         
00050                 do 
00051                         lm++;
00052                 while(data[lm] < val);
00053 
00054                 if(lm < rm)
00055         {
00056                         Type tempr = data[rm];
00057                         data[rm] = data[lm];
00058                         data[lm] = tempr;
00059                 }
00060                 else 
00061                         return rm;
00062         }
00063 }
00064 
00066 template<class Type>
00067 void QuickSort(Type* data, int left, int right)
00068 {
00069         if(left < (right-cQuicksortTreshold))
00070     {
00071                 int split_pt = Partition(data,left, right);
00072                 QuickSort(data, left, split_pt);
00073                 QuickSort(data, split_pt+1, right);
00074         }
00075         else 
00076         SelectionSort(data, left, right);
00077 }
00078 
00079 
00081 //    normal quicksort and selection sort        //
00082 //            in descending order                //
00084 
00086 template<class Type>
00087 void SelectionSortDesc(Type data[], int left, int right)
00088 {
00089         for(int i=left ; i<right ; i++)
00090     {
00091                 int max = i;
00092                 for(int j=i+1 ; j<=right ; j++)
00093                         if(data[j] > data[max])
00094                 max = j;
00095                 Type temp = data[max];
00096                 data[max] = data[i];
00097                 data[i] = temp;
00098         }
00099 }
00100 
00102 template<class Type>
00103 int PartitionDesc(Type* data, int left, int right)
00104 {
00105         Type val = data[left];
00106         int lm = left-1;
00107         int rm = right+1;
00108         for(;;)
00109     {
00110                 do
00111                         rm--;
00112                 while (data[rm] < val);
00113         
00114                 do 
00115                         lm++;
00116                 while(data[lm] > val);
00117 
00118                 if(lm < rm)
00119         {
00120                         Type tempr = data[rm];
00121                         data[rm] = data[lm];
00122                         data[lm] = tempr;
00123                 }
00124                 else 
00125                         return rm;
00126         }
00127 }
00128 
00130 template<class Type>
00131 void QuickSortDesc(Type* data, int left, int right)
00132 {
00133         if(left < (right-cQuicksortTreshold))
00134     {
00135                 int split_pt = PartitionDesc(data,left, right);
00136                 QuickSortDesc(data, left, split_pt);
00137                 QuickSortDesc(data, split_pt+1, right);
00138         }
00139         else 
00140         SelectionSortDesc(data, left, right);
00141 }
00142 
00144 // quicksort and selection sort with 'companion' //
00145 //      used in sorting tables and stuff         //
00147 
00149 template<class Type, class CoType>
00150 void SelectionSortCo(Type* data, CoType* coData, int left, int right)
00151 {
00152         for(int i=left ; i<right ; i++)
00153     {
00154                 int min = i;
00155                 for(int j=i+1 ; j<=right ; j++)
00156                         if(data[j] < data[min])
00157                 min = j;
00158                 Type temp = data[min];
00159                 data[min] = data[i];
00160                 data[i] = temp;
00161                 CoType coTemp = coData[min];
00162                 coData[min] = coData[i];
00163                 coData[i] = coTemp;
00164         }
00165 }
00166 
00168 template<class Type, class CoType>
00169 int PartitionCo(Type* data, CoType* coData, int left, int right)
00170 {
00171         Type val = data[left];
00172         int lm = left-1;
00173         int rm = right+1;
00174         for(;;)
00175     {
00176                 do
00177                         rm--;
00178                 while (data[rm] > val);
00179         
00180                 do 
00181                         lm++;
00182                 while(data[lm] < val);
00183 
00184                 if(lm < rm)
00185         {
00186                         Type temp = data[rm];
00187                         data[rm] = data[lm];
00188                         data[lm] = temp;
00189                         CoType coTemp = coData[rm];
00190                         coData[rm] = coData[lm];
00191                         coData[lm] = coTemp;
00192                 }
00193                 else 
00194                         return rm;
00195         }
00196 }
00197 
00199 template<class Type, class CoType>
00200 void QuickSortCo(Type* data, CoType* coData, int left, int right)
00201 {
00202         if(left < (right-cQuicksortTreshold))
00203     {
00204                 int split_pt = PartitionCo(data, coData, left, right);
00205                 QuickSortCo(data, coData, left, split_pt);
00206                 QuickSortCo(data, coData, split_pt+1, right);
00207         }
00208         else 
00209         SelectionSortCo(data, coData, left, right);
00210 }
00211 
00213 // quicksort and selection sort with 'companion' //
00214 //           in descending order                 //
00215 //      used in sorting tables and stuff         //
00217 
00219 template<class Type, class CoType>
00220 void SelectionSortDescCo(Type* data, CoType* coData, int left, int right)
00221 {
00222         for(int i=left ; i<right ; i++)
00223     {
00224                 int max = i;
00225                 for(int j=i+1 ; j<=right ; j++)
00226                         if(data[j] > data[max])
00227                 max = j;
00228                 Type temp = data[max];
00229                 data[max] = data[i];
00230                 data[i] = temp;
00231                 CoType coTemp = coData[max];
00232                 coData[max] = coData[i];
00233                 coData[i] = coTemp;
00234         }
00235 }
00236 
00238 template<class Type, class CoType>
00239 int PartitionDescCo(Type* data, CoType* coData, int left, int right)
00240 {
00241         Type val = data[left];
00242         int lm = left-1;
00243         int rm = right+1;
00244         for(;;)
00245     {
00246                 do
00247                         rm--;
00248                 while (data[rm] < val);
00249         
00250                 do 
00251                         lm++;
00252                 while(data[lm] > val);
00253 
00254                 if(lm < rm)
00255         {
00256                         Type temp = data[rm];
00257                         data[rm] = data[lm];
00258                         data[lm] = temp;
00259                         CoType coTemp = coData[rm];
00260                         coData[rm] = coData[lm];
00261                         coData[lm] = coTemp;
00262                 }
00263                 else 
00264                         return rm;
00265         }
00266 }
00267 
00269 template<class Type, class CoType>
00270 void QuickSortDescCo(Type* data, CoType* coData, int left, int right)
00271 {
00272         if(left < (right-cQuicksortTreshold))
00273     {
00274                 int split_pt = PartitionDescCo(data, coData, left, right);
00275                 QuickSortDescCo(data, coData, left, split_pt);
00276                 QuickSortDescCo(data, coData, split_pt+1, right);
00277         }
00278         else 
00279         SelectionSortDescCo(data, coData, left, right);
00280 }
00281 
00282 } // namespace Util
00283 } // namespace Impala
00284 
00285 #endif // Impala_Util_QuickSort_h

Generated on Fri Mar 19 09:31:47 2010 for ImpalaSrc by  doxygen 1.5.1