00001 #ifndef __J2K__Sort__OldQuickSort_CPP__
00002 #define __J2K__Sort__OldQuickSort_CPP__
00003
00004 #include "Sort.hpp"
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 void OldQuickSort( Elem* array, ulong Size )
00017 {
00018 struct QEntry QStack[100];
00019
00020 long Top = 0;
00021 long Bottom = 0;
00022
00023 long a = 0;
00024 long b = Size-1;
00025
00026 Elem z;
00027
00028 QStack[0].Left = a;
00029 QStack[0].Right = b;
00030
00031 long x = 1;
00032
00033 while ( x > 0 )
00034 {
00035 x--;
00036 a = QStack[x].Left;
00037 b = QStack[x].Right;
00038
00039 z = array[a];
00040
00041 Top = a;
00042 Bottom = b + 1;
00043
00044 QSBottom:
00045
00046 Bottom--;
00047
00048 if ( Bottom == Top ) goto QSz;
00049
00050 if ( z <= array[Bottom] )
00051 {
00052 goto QSBottom;
00053 }
00054 else
00055 {
00056 array[Top] = array[Bottom];
00057 }
00058
00059 QSTop:
00060
00061 Top++;
00062
00063 if ( Bottom == Top) goto QSz;
00064
00065 if ( z >= array[ Top ] )
00066 {
00067 goto QSTop;
00068 }
00069 else
00070 {
00071 array[ Bottom ] = array[ Top ];
00072 goto QSBottom;
00073 }
00074
00075 QSz:
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
00096
00097
00098
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;
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
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 }
00178
00179 #endif // End of OldQSort.cpp