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

C:/temp/src/j2k/Test/Sort_Test.cpp

Go to the documentation of this file.
00001 // Main.cpp - main program declaration.
00002 
00003 #include <j2k/DataType/Const.hpp>
00004 #include <j2k/DataType/Sort/Random.hpp>
00005 #include <j2k/DataType/Sort/Sort.hpp>
00006 
00007 void SelSort2( Elem* array1, Elem* array2, int n );
00008 
00009 // If everything run correctly the program return 0
00010 // else the program return the errorcode level 1
00011 // to indicate that the datafile was not found
00012 // in the current directory.
00013 
00014 int main( void )
00015 {
00016    ulong SIZE = (ulong)pow( 2, 15 );
00017    ulong MAX  = (ulong)pow( 2, 20 );
00018 
00019    int TEST = 150;
00020    int i = 0;
00021 
00022    printf("SIZE=[%7d]  ", SIZE );
00023    printf("MAX=[%7d] \n", MAX  );
00024 
00025    Random* r = new Random( SIZE, MAX  );
00026 
00027    char* SortName[9];
00028 
00029    SortName[0] = "InsertSort";
00030    SortName[1] = "BubbleSort";
00031    SortName[2] = "SelectionSort";
00032    SortName[3] = "ShellSort";
00033    SortName[4] = "Merge Sort";
00034    SortName[5] = "Heap Sort";
00035    SortName[6] = "Radix Sort";
00036    SortName[7] = "Quick Sort";
00037    SortName[8] = "Old Quick Sort";
00038 
00039 
00040    clock_t start, end;
00041    //  clock_t allstart, allend, alldelay;
00042    //  double  allsec;
00043 
00044    Elem  elapsed;
00045    Elem* delay = new Elem[9];
00046    Elem* Index = new Elem[9];
00047 
00048    for( i = 0; i < 9; i++ ) 
00049    {
00050       delay[i] = 0;
00051       Index[i] = i;
00052    }
00053 
00054    for( i = 0; i < 4; i++ )  delay[i] = 99999;
00055 
00056    
00057    for( i = 0; i < TEST; i++ ) 
00058    {
00059          allstart = clock();
00060 
00061          r->feed( false );
00062          // printf( "\nRandom Data:  \n");
00063          // r->Display();
00064   
00065          printf( "InsertSort:  ");
00066 
00067          start = clock();
00068          InsertionSort( r->getArray(), r->getSize() );
00069          end   = clock();
00070          elapsed = end - start;
00071          delay[0] += elapsed;
00072          printf( "\t\t%6d  clock ticks\n", elapsed );
00073 
00074          // r->Display();
00075          r->Restore();
00076 
00077          printf( "BubbleSort:  ");
00078 
00079          start = clock();
00080          BubbleSort( r->getArray(), r->getSize() );
00081          end   = clock();
00082          elapsed = end - start;
00083          delay[1] += elapsed;
00084          printf( "\t\t%6d  clock ticks\n", elapsed );
00085 
00086          // r->Display();
00087             r->Restore();
00088 
00089          printf( "SelectionSort:");
00090 
00091          start = clock();
00092          SelectionSort( r->getArray(), r->getSize() );
00093          end   = clock();
00094          elapsed = end - start;
00095          delay[2] += elapsed;
00096          printf( "\t\t%6d  clock ticks\n", elapsed );
00097 
00098        // r->Display();      
00099             r->Restore();
00100 
00101          printf( "ShellSort:  ");
00102 
00103          start = clock();
00104          ShellSort( r->getArray(), r->getSize() );
00105          end   = clock();
00106          elapsed = end - start;
00107          delay[3] += elapsed;
00108          printf( "\t\t%6d  clock ticks\n", elapsed );
00109 
00110          printf( "Merge Sort:  ");
00111 
00112          start = clock();
00113          MergeSort( r->getArray(), r->getSize() );
00114          end   = clock();
00115          elapsed = end - start;
00116          delay[4] += elapsed;
00117          printf( "\t\t%6d  clock ticks\n", elapsed );
00118 
00119        // r->Display();      
00120          r->Restore();
00121        
00122          printf( "Heap Sort:  ");
00123 
00124          start = clock();
00125          HeapSort( r->getArray(), r->getSize() );
00126          end   = clock();
00127          elapsed = end - start;
00128          delay[5] += elapsed;
00129          printf( "\t\t%6d  clock ticks\n", elapsed );
00130 
00131 
00132        // r->Display();      
00133          r->Restore();
00134 
00135          printf( "Radix Sort:  ");
00136 
00137          start = clock();
00138          RadixSort( r->getArray(), r->getSize() );
00139          end   = clock();
00140          elapsed = end - start;
00141          delay[6] += elapsed;
00142          printf( "\t\t%6d  clock ticks\n", elapsed );
00143 
00144 
00145             // r->Display();      
00146             r->Restore();
00147 
00148          printf( "Quick Sort:  ");
00149 
00150          start = clock();
00151          QuickSort( r->getArray(), r->getSize() );
00152          end   = clock();
00153          elapsed = end - start;
00154          delay[7] += elapsed;
00155          printf( "\t\t%6d  clock ticks\n", elapsed );
00156 
00157 
00158        // r->Display();      
00159             r->Restore();
00160 
00161          printf( "Old Quick Sort:");
00162 
00163          start = clock();
00164          OldQuickSort( r->getArray(), r->getSize() );
00165          end   = clock();
00166          elapsed = end - start;
00167          delay[8] += elapsed;
00168          printf( "\t\t%6d  clock ticks\n", elapsed );
00169 
00170        // r->Display();  
00171        
00172 //     allend = clock();
00173 //     alldelay = allend - allstart;
00174 //     allsec = alldelay / CLOCKS_PER_SEC;
00175 //     printf("\n It takes  %6d  clock ticks ");
00176 //     printf("or %.2f to run it once.\n\n", alldelay, allsec );
00177    }
00178 
00179    delete r;
00180 
00181    SelSort2( delay, Index, 9 );
00182 
00183    double unit = delay[ 0 ];
00184    double t = 0;
00185 
00186    if ( unit < 1 ) { unit = 1; }
00187    
00188    for( i = 0; i < 9; i++ ) 
00189    {
00190      t = (double)delay[ i ] / unit;
00191      printf( "\nFor %s the average number of ", SortName[ Index[i] ] );
00192      printf( "clock ticks is \t %6d or %.2f time unit ", (delay[ i ] / TEST), t );
00193    }
00194 
00195    return 0;
00196 }
00197 
00198 // Selection Sort Algorithm
00199 void SelSort2( Elem* array1, Elem* array2, int n )
00200 {
00201   n--;  // n-1 optimization
00202 
00203   // Select i'th record
00204   for( register int i = 0; i < n; i++ ) 
00205   {
00206     int lowindex = i;                      // Remember its index
00207     for( register int j = n; j > i; j-- )  // Find the least value
00208     {
00209      if ( array1[j] < array1[lowindex] )
00210       {
00211          lowindex = j;                 // Put it in place
00212       }
00213     }
00214 
00215     swap( array1, i, lowindex );
00216     swap( array2, i, lowindex );
00217   }
00218 }

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