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