Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "sort.h"
  3.  
  4. //#define _DEBUG_
  5. //#define _DEBUG2_
  6.  
  7.  
  8. typedef void ( *pF ) ( int *, int );
  9.  
  10. clock_t T;
  11.  
  12. void InitTab( int*, int );
  13. void PrintTab( int*, int );
  14. void Test( pF* pSortFun, const char* pMethodNames[], int N, int* pPat, int nSize );
  15.  
  16. int main( int argc, char* argv[] )
  17. {
  18.    
  19.     if( argc !=2 )
  20.     {
  21.       printf( "attention: error");
  22.      return 1;
  23.     }
  24.    
  25.     int nSize = atoi( argv[ 1 ] );
  26.  
  27. #ifdef _DEBUG_
  28.     printf ( "Generating Tab of %d elements...\n\n", nSize );
  29. #endif
  30.  
  31.     int* pPat = ( int* )malloc( nSize * sizeof( int ) );
  32.     if( !pPat )
  33.     {
  34.         printf( "error: pPat not allocated" );
  35.         return 1;
  36.     }
  37.  
  38.     memset( pPat, 0, nSize * sizeof( int ) );
  39.  
  40.     InitTab( pPat, nSize ); //initiation Tab with random numbers
  41.    
  42.     #ifdef _DEBUG2_
  43.     PrintTab( pPat, nSize );
  44.     #endif
  45.  
  46.  
  47.     pF pSortFun[] = { InsertionSort,SelectionSort,HalfSort,MixSort,HeapSort }; // MergeSortSeq
  48.     int N = sizeof( pSortFun ) / ( sizeof( pF ) );
  49.    
  50.     const char* pMethodNames[] = { "InsertionSort","SelectionSort","HalfSort","MixSort","HeapSort"}; //"MergeSortSeq"
  51.  
  52. #ifdef _DEBUG_
  53.     printf ( "Sorting Tab of %d elements:\n", nSize );
  54. #endif
  55.     Test( pSortFun, pMethodNames, N, pPat, nSize );
  56.  
  57.  
  58.     int* pTab = ( int* )malloc( nSize * sizeof( int ) );
  59.     if( !pTab )
  60.     {
  61.         printf( "error: pTab not allocated" );
  62.         return 1;
  63.     }
  64.         memset( pTab, 0, nSize * sizeof( int ) );
  65.  
  66.     int* ptmp = ( int* )malloc( nSize * sizeof( int ) );
  67.     if( !ptmp )
  68.     {
  69.         printf( "error: ptmp not allocated" );
  70.         return 1;
  71.     }
  72.         memset( ptmp, 0, nSize * sizeof( int ) );
  73.  
  74.    
  75. #ifdef _DEBUG_
  76.     printf ( "QuickSort...Please wait... \n " );
  77. #endif
  78.     memcpy( pTab, pPat, nSize * sizeof( int ) ); //copy pPat to pTab
  79. #ifdef _DEBUG_
  80.     printf( "Patern Tab \n" );
  81. #endif
  82.     #ifdef _DEBUG_
  83.     PrintTab( pTab, nSize );
  84.     #endif
  85.     T=clock();
  86.     QuickSort( pTab, 0, nSize - 1 );
  87.     T = 1000 / CLOCKS_PER_SEC*( clock() - T );
  88.     printf( " Quicksort time:  %lu ms\n", T );
  89.     PrintTab( pTab, nSize );
  90.    
  91.  
  92. #ifdef _DEBUG_
  93.     printf ( "MergeSort...Please wait... \n " );
  94. #endif
  95.     memcpy( pTab, pPat, nSize * sizeof( int ) ); //copy pPat to pTab
  96. #ifdef _DEBUG_
  97.     printf( "Patern Tab \n" );
  98. #endif
  99. #ifdef _DEBUG_
  100.     PrintTab( pTab, nSize );
  101. #endif
  102.     T = clock();
  103.     MergeSort( pTab, ptmp,nSize, 0, nSize - 1 );
  104.     T = 1000 / CLOCKS_PER_SEC*( clock() - T );
  105.     printf( " MergeSort time:  %lu ms\n", T );
  106. #ifdef _DEBUG_
  107.     PrintTab( pTab, nSize );
  108. #endif
  109.    
  110.  
  111.     free( pTab );
  112.     free( pPat );
  113.     free( ptmp );
  114.     getchar();
  115.         return 0;
  116.     }
  117.  
  118.  
  119.  
  120. void InitTab( int* pPat , int nSize ) //inicjacja tablicy z pomoca generatora liczb losowych
  121. {
  122.     srand( unsigned int ( time( NULL ) ) );
  123.         for( int i = 0; i < nSize; i++ ) //use pointers
  124.         {
  125.             *pPat++ = rand() % 10000;
  126.         }
  127. }
  128.  
  129. void PrintTab( int* pPat, int nSize )
  130. {
  131.     for( int i = 0; i < nSize; i++ )
  132.         {
  133.             printf( "\t%i", *pPat++ );
  134.         }
  135.     printf( "\n" );
  136. }
  137.  
  138.  
  139. void Test( pF* pSortFun, const char* pMethodNames[], int N, int* pPat, int nSize ) //char ** pMethodNames
  140. {
  141.     int* pTab = ( int* )malloc( nSize * sizeof( int ) );
  142.     if( !pTab )
  143.     {
  144.         printf( "error: pTab not allocated" );
  145.         return ;
  146.     }
  147.     else
  148.         memset( pTab, 0, nSize * sizeof( int ) );
  149.    
  150.  
  151.     for( int i = 0; i < N; i++ )
  152.     {
  153.         memcpy( pTab, pPat, nSize * sizeof( int ) );
  154.         T= clock();
  155.         pSortFun[ i ]( pTab, nSize ); //wywolanie funkcji pierwszej w tablicy
  156.         T = 1000 / CLOCKS_PER_SEC*( clock() - T );
  157.         printf ( "%s :  %lu ms \n", pMethodNames[ i ], T );
  158.     #ifdef _DEBUG_
  159.         PrintTab( pTab, nSize );
  160.     #endif
  161.  
  162.        
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement