Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "sort.h"
- //#define _DEBUG_
- //#define _DEBUG2_
- typedef void ( *pF ) ( int *, int );
- clock_t T;
- void InitTab( int*, int );
- void PrintTab( int*, int );
- void Test( pF* pSortFun, const char* pMethodNames[], int N, int* pPat, int nSize );
- int main( int argc, char* argv[] )
- {
- if( argc !=2 )
- {
- printf( "attention: error");
- return 1;
- }
- int nSize = atoi( argv[ 1 ] );
- #ifdef _DEBUG_
- printf ( "Generating Tab of %d elements...\n\n", nSize );
- #endif
- int* pPat = ( int* )malloc( nSize * sizeof( int ) );
- if( !pPat )
- {
- printf( "error: pPat not allocated" );
- return 1;
- }
- memset( pPat, 0, nSize * sizeof( int ) );
- InitTab( pPat, nSize ); //initiation Tab with random numbers
- #ifdef _DEBUG2_
- PrintTab( pPat, nSize );
- #endif
- pF pSortFun[] = { InsertionSort,SelectionSort,HalfSort,MixSort,HeapSort }; // MergeSortSeq
- int N = sizeof( pSortFun ) / ( sizeof( pF ) );
- const char* pMethodNames[] = { "InsertionSort","SelectionSort","HalfSort","MixSort","HeapSort"}; //"MergeSortSeq"
- #ifdef _DEBUG_
- printf ( "Sorting Tab of %d elements:\n", nSize );
- #endif
- Test( pSortFun, pMethodNames, N, pPat, nSize );
- int* pTab = ( int* )malloc( nSize * sizeof( int ) );
- if( !pTab )
- {
- printf( "error: pTab not allocated" );
- return 1;
- }
- memset( pTab, 0, nSize * sizeof( int ) );
- int* ptmp = ( int* )malloc( nSize * sizeof( int ) );
- if( !ptmp )
- {
- printf( "error: ptmp not allocated" );
- return 1;
- }
- memset( ptmp, 0, nSize * sizeof( int ) );
- #ifdef _DEBUG_
- printf ( "QuickSort...Please wait... \n " );
- #endif
- memcpy( pTab, pPat, nSize * sizeof( int ) ); //copy pPat to pTab
- #ifdef _DEBUG_
- printf( "Patern Tab \n" );
- #endif
- #ifdef _DEBUG_
- PrintTab( pTab, nSize );
- #endif
- T=clock();
- QuickSort( pTab, 0, nSize - 1 );
- T = 1000 / CLOCKS_PER_SEC*( clock() - T );
- printf( " Quicksort time: %lu ms\n", T );
- PrintTab( pTab, nSize );
- #ifdef _DEBUG_
- printf ( "MergeSort...Please wait... \n " );
- #endif
- memcpy( pTab, pPat, nSize * sizeof( int ) ); //copy pPat to pTab
- #ifdef _DEBUG_
- printf( "Patern Tab \n" );
- #endif
- #ifdef _DEBUG_
- PrintTab( pTab, nSize );
- #endif
- T = clock();
- MergeSort( pTab, ptmp,nSize, 0, nSize - 1 );
- T = 1000 / CLOCKS_PER_SEC*( clock() - T );
- printf( " MergeSort time: %lu ms\n", T );
- #ifdef _DEBUG_
- PrintTab( pTab, nSize );
- #endif
- free( pTab );
- free( pPat );
- free( ptmp );
- getchar();
- return 0;
- }
- void InitTab( int* pPat , int nSize ) //inicjacja tablicy z pomoca generatora liczb losowych
- {
- srand( unsigned int ( time( NULL ) ) );
- for( int i = 0; i < nSize; i++ ) //use pointers
- {
- *pPat++ = rand() % 10000;
- }
- }
- void PrintTab( int* pPat, int nSize )
- {
- for( int i = 0; i < nSize; i++ )
- {
- printf( "\t%i", *pPat++ );
- }
- printf( "\n" );
- }
- void Test( pF* pSortFun, const char* pMethodNames[], int N, int* pPat, int nSize ) //char ** pMethodNames
- {
- int* pTab = ( int* )malloc( nSize * sizeof( int ) );
- if( !pTab )
- {
- printf( "error: pTab not allocated" );
- return ;
- }
- else
- memset( pTab, 0, nSize * sizeof( int ) );
- for( int i = 0; i < N; i++ )
- {
- memcpy( pTab, pPat, nSize * sizeof( int ) );
- T= clock();
- pSortFun[ i ]( pTab, nSize ); //wywolanie funkcji pierwszej w tablicy
- T = 1000 / CLOCKS_PER_SEC*( clock() - T );
- printf ( "%s : %lu ms \n", pMethodNames[ i ], T );
- #ifdef _DEBUG_
- PrintTab( pTab, nSize );
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement