0001 0002 /****************************************************************************************** 0003 * 构造随机数组(可根据测试需要相应地改写) 0004 ******************************************************************************************/ 0005 void randomArray ( int A[], int n, int seed ) { 0006 srand(seed); 0007 for (int i = 0; i < n; i++) A[i] = i; 0008 shuffle(A, n); 0009 } 0010 0011 /****************************************************************************************** 0012 * 起泡排序测试程序 0013 ******************************************************************************************/ 0014 int main ( int argc, char* argv[] ) { 0015 if ( 2 > argc ) { printf ( "Usage: %s <size of test>\a\a\n", argv[0] ); return 1; } 0016 int n = atoi ( argv[1] ); if ( n < 0 ) n = 0; //make sure length is non-negative 0017 int* A = new int[ n ]; //allocate an array of size n 0018 unsigned int seed = ( unsigned int ) time ( NULL ); //A same seed is used here to compare different algorithms 0019 printf ( "\n== Bubblesort algorithm #0 ========\n" ); 0020 randomArray ( A, n, seed ); //create a randomized array using the same seed 0021 printf ( "--> " ); print ( A, n ); 0022 bubblesort ( A, n ); //sort the array using algorithm#0 0023 printf ( "==> " ); print ( A, n ); 0024 printf ( "\n== Bubblesort algorithm #1A ========\n" ); 0025 randomArray ( A, n, seed ); //create a randomized array using the same seed 0026 printf ( "--> " ); print ( A, n ); 0027 bubblesort1A ( A, n ); //sort the array using algorithm#1A 0028 printf ( "==> " ); print ( A, n ); 0029 printf ( "\n== Bubblesort algorithm #1B ========\n" ); 0030 randomArray ( A, n, seed ); //create a randomized array using the same seed 0031 printf ( "--> " ); print ( A, n ); 0032 bubblesort1B ( A, n ); //sort the array using algorithm#1B 0033 printf ( "==> " ); print ( A, n ); 0034 printf ( "\n== Bubblesort algorithm #2 ========\n" ); 0035 randomArray ( A, n, seed ); //create a randomized array using the same seed 0036 printf ( "--> " ); print ( A, n ); 0037 bubblesort2 ( A, n ); //sort the array using algorithm#2 0038 printf ( "==> " ); print ( A, n ); 0039 delete [] A; 0040 return 0; 0041 }