Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/Sort/OldQsort.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__Sort__OldQuickSort_CPP__
00002 #define __J2K__Sort__OldQuickSort_CPP__
00003 
00004 #include "Sort.hpp"
00005 
00006 /* Goto Statement are used as a short and fast way
00007    to do recursion inside this sort algorithm.
00008 
00009    The difference might not seems to be significant
00010    in this version of C++, but in some other language or version
00011    where recursivity is not optimize by the compiler,
00012    this version show a significant improvement over the standard
00013    QuickSort by as much as 80x faster in Visual Basic 5.0 !
00014 */
00015 
00016 void OldQuickSort( Elem* array, ulong Size )
00017 {                                 
00018   struct QEntry QStack[100];        // Use to store variables for recursivity
00019 
00020   long Top = 0;                     // Top & Bottom ptr
00021   long Bottom = 0;
00022 
00023   long a = 0;                       // Inner Left & Right working ptr
00024   long b = Size-1;                       
00025  
00026   Elem z;                           // Temp location for swapping stuff
00027  
00028   QStack[0].Left  = a;              // Define the absolute left ptr 
00029   QStack[0].Right = b;              // Define the absolute right ptr
00030 
00031   long x = 1;                       // Normal Middle ptr
00032 
00033   while ( x > 0 )
00034   {
00035     x--;
00036     a = QStack[x].Left;             // Store Left and Right Stack value
00037     b = QStack[x].Right;
00038 
00039     z = array[a];                   // Give me some z value
00040    
00041     Top    = a;                     // Fix Top and Bottom range
00042     Bottom = b + 1;
00043 
00044 QSBottom: // Address #1  -- Let's play with Bottom !
00045 
00046     Bottom--;
00047 
00048     if ( Bottom == Top ) goto QSz;  // If there is no middle than fix z
00049 
00050     if ( z <= array[Bottom] )       // If z is smaller than bottom-- and retry
00051     {    
00052       goto QSBottom;
00053     } 
00054     else 
00055     { 
00056       array[Top] = array[Bottom];
00057     }
00058 
00059 QSTop:  // Address #2  -- Let's play with Top !
00060 
00061     Top++;
00062 
00063     if ( Bottom == Top) goto QSz;    // If there is no middle than fix z
00064 
00065     if ( z >= array[ Top ] )         // If z is bigger than top++ and retry
00066     {       
00067       goto QSTop;
00068     } 
00069     else 
00070     {
00071       array[ Bottom ] = array[ Top ];
00072       goto QSBottom;
00073     }
00074 
00075 QSz:  // Address #3  -- Let's fix z and continue.
00076  
00077     array[Top] = z;
00078 
00079     if ( (b - Top) >= 2 )
00080     {
00081       QStack[x].Left  = Top + 1;
00082       QStack[x].Right = b;
00083       x++;
00084     }
00085 
00086     if ( (Bottom - a) >= 2 )
00087     {
00088       QStack[x].Left  = a;
00089       QStack[x].Right = Bottom - 1;
00090       x++;
00091     }
00092   }
00093 }
00094 
00095 // Take 20 value equally spread in the array and check for:
00096 // - increasing ordered list, if so use InsertionSort,
00097 // - decreasing ordered list, if so reverse and use InsertionSort,
00098 // - Quite messy non-ordered list, than use OldQuickSort
00099 void BrightQSort( Elem* array, ulong Size )
00100 {
00101    ULONG* ptr = new ULONG[20];
00102    int   l = 0;
00103    int   r = 0;
00104    register int i = 0;
00105    ULONG k = 0;
00106 
00107    int   Trigger;
00108 
00109    Elem  T;
00110 
00111    int Test   = 20;
00112    int Test34 = Test * 3 / 4;     // 75% Ratio to be declare sorted
00113 
00114    int n  = Size / Test;
00115    int n2 = (int)floor(n / 2);
00116 
00117    for( i = 1; i <= Test; i++ )
00118    {
00119       ptr[i] = (ulong)floor( n * i );
00120    }
00121 
00122    for( i = 2; i <= Test; i++ )
00123    {
00124      T = array[ ptr[i] ] - array[ ptr[i - 1] - n2 ];
00125 
00126        if ( T < 0 )
00127        {
00128             l++;
00129        } 
00130        else 
00131        {
00132             r++;
00133        }
00134 
00135        printf( "[%d, %d, %d]", T, l, r );
00136    }
00137 
00138    printf( "[%d, %d, %d]", T, l, r );
00139 
00140    Trigger = r - l;
00141 
00142    printf( "%d", Trigger );
00143 
00144 
00145    if ( Trigger > Test34 )
00146    {
00147      printf( "INC" );
00148      InsertionSort( array, Size );
00149      return;
00150    }
00151 
00152 
00153    if ( Trigger < (-1 * Test34) )
00154    {
00155       printf("DEC");
00156   
00157       // Reverse the array
00158       for( k = 1; k <= ( Size / 2 + 1 ); k++ )
00159       {
00160          swap( array, k, Size - k + 1 );
00161       }
00162 
00163       for( k = 1; k <= Size; k++ )
00164       {
00165          printf("%d", array[k] );
00166       }
00167 
00168       InsertionSort( array, Size );
00169       return;
00170    }
00171 
00172 
00173    printf( "RND" );
00174 
00175    OldQuickSort( array, Size );
00176 
00177 } // End of BrightQSort()
00178 
00179 #endif // End of OldQSort.cpp

Generated on Sun Oct 14 18:46:18 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001