1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| typedef struct { int comparisons; int movements; double time_taken; int bucket_count; } BucketSortStats;
void bucket_sort_adaptive(double arr[], int n, BucketSortStats* stats) { clock_t start = clock(); stats->comparisons = 0; stats->movements = 0; int bucket_count = n / 5; if (bucket_count < 1) bucket_count = 1; if (bucket_count > n) bucket_count = n; stats->bucket_count = bucket_count; double min_val = arr[0], max_val = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] < min_val) min_val = arr[i]; if (arr[i] > max_val) max_val = arr[i]; } double range = max_val - min_val; if (range == 0) return; Bucket* buckets = (Bucket*)calloc(bucket_count, sizeof(Bucket)); for (int i = 0; i < n; i++) { int bucket_index = (int)((arr[i] - min_val) / range * (bucket_count - 1)); insert_to_bucket(&buckets[bucket_index], arr[i]); stats->movements++; } int index = 0; for (int i = 0; i < bucket_count; i++) { BucketNode* current = buckets[i].head; while (current != NULL) { arr[index++] = current->data; stats->movements++; BucketNode* temp = current; current = current->next; free(temp); } } free(buckets); clock_t end = clock(); stats->time_taken = ((double)(end - start)) / CLOCKS_PER_SEC; }
void performance_analysis() { printf("=== 桶排序性能分析 ===\n"); int sizes[] = {100, 500, 1000}; printf("%-10s %-10s %-12s %-12s %-15s\n", "数据量", "桶数量", "移动次数", "时间(秒)", "平均桶大小"); printf("--------------------------------------------------------\n"); for (int s = 0; s < 3; s++) { int n = sizes[s]; double* arr = (double*)malloc(n * sizeof(double)); srand(42); for (int i = 0; i < n; i++) { arr[i] = (double)rand() / RAND_MAX; } BucketSortStats stats; bucket_sort_adaptive(arr, n, &stats); double avg_bucket_size = (double)n / stats.bucket_count; printf("%-10d %-10d %-12d %-12.6f %-15.1f\n", n, stats.bucket_count, stats.movements, stats.time_taken, avg_bucket_size); free(arr); } printf("\n"); }
void test_different_distributions() { printf("=== 不同数据分布测试 ===\n"); const int n = 20; double uniform[n]; srand(42); for (int i = 0; i < n; i++) { uniform[i] = (double)rand() / RAND_MAX; } printf("均匀分布数据:\n"); printf("排序前: "); for (int i = 0; i < n; i++) printf("%.2f ", uniform[i]); printf("\n"); BucketSortStats stats; bucket_sort_adaptive(uniform, n, &stats); printf("排序后: "); for (int i = 0; i < n; i++) printf("%.2f ", uniform[i]); printf("\n"); printf("使用 %d 个桶,移动 %d 次\n\n", stats.bucket_count, stats.movements); double concentrated[n]; for (int i = 0; i < n; i++) { concentrated[i] = 0.4 + ((double)rand() / RAND_MAX) * 0.2; } printf("集中分布数据 [0.4, 0.6]:\n"); printf("排序前: "); for (int i = 0; i < n; i++) printf("%.2f ", concentrated[i]); printf("\n"); bucket_sort_adaptive(concentrated, n, &stats); printf("排序后: "); for (int i = 0; i < n; i++) printf("%.2f ", concentrated[i]); printf("\n"); printf("使用 %d 个桶,移动 %d 次\n\n", stats.bucket_count, stats.movements); }
|