fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6. #include <cmath> // 用于计算阶乘
  7.  
  8. using namespace std;
  9.  
  10. // 计算阶乘(用于时间复杂度估计)
  11. long long factorial(int n) {
  12. if (n <= 1) return 1;
  13. return n * factorial(n - 1);
  14. }
  15.  
  16. // 检查数组是否已排序
  17. bool isSorted(const vector<int>& arr) {
  18. for (size_t i = 0; i < arr.size() - 1; ++i) {
  19. if (arr[i] > arr[i + 1]) {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25.  
  26. // 猴子排序(带时间复杂度输出)
  27. void bogoSort(vector<int>& arr) {
  28. auto startTime = chrono::high_resolution_clock::now(); // 记录开始时间
  29. unsigned seed = chrono::system_clock::now().time_since_epoch().count();
  30. default_random_engine engine(seed);
  31.  
  32. int attempts = 0;
  33. const int n = arr.size();
  34. const long long worstCaseComplexity = factorial(n); // 最坏情况 O(n!)
  35.  
  36. cout << "猴子排序(Bogo Sort)时间复杂度分析:" << endl;
  37. cout << "数组大小 n = " << n << endl;
  38. cout << "最坏情况时间复杂度: O(n!) = " << worstCaseComplexity << " 次尝试" << endl;
  39. cout << "平均情况时间复杂度: O(n * n!)" << endl;
  40. cout << "----------------------------------" << endl;
  41.  
  42. while (!isSorted(arr)) {
  43. shuffle(arr.begin(), arr.end(), engine);
  44. attempts++;
  45.  
  46. // 每10000次尝试输出一次当前状态
  47. if (attempts % 10000 == 0) {
  48. auto currentTime = chrono::high_resolution_clock::now();
  49. auto elapsedTime = chrono::duration_cast<chrono::milliseconds>(currentTime - startTime).count();
  50.  
  51. cout << "已尝试 " << attempts << " 次 | ";
  52. cout << "当前时间复杂度: O(" << attempts << ") | ";
  53. cout << "已运行时间: " << elapsedTime << " ms" << endl;
  54. }
  55. }
  56.  
  57. auto endTime = chrono::high_resolution_clock::now();
  58. auto totalTime = chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count();
  59.  
  60. cout << "----------------------------------" << endl;
  61. cout << "排序完成!" << endl;
  62. cout << "实际尝试次数: " << attempts << endl;
  63. cout << "理论最坏情况尝试次数: O(n!) = " << worstCaseComplexity << endl;
  64. cout << "总运行时间: " << totalTime << " ms" << endl;
  65. }
  66.  
  67. int main() {
  68. vector<int> arr = {3, 1, 4, 1, 5, 9,4,76,346}; // 测试数组(建议n≤6,否则可能极慢)
  69.  
  70. cout << "排序前: ";
  71. for (int num : arr) cout << num << " ";
  72. cout << endl;
  73.  
  74. bogoSort(arr);
  75.  
  76. cout << "排序后: ";
  77. for (int num : arr) cout << num << " ";
  78. cout << endl;
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0.02s 5284KB
stdin
Standard input is empty
stdout
排序前: 3 1 4 1 5 9 4 76 346 
猴子排序(Bogo Sort)时间复杂度分析:
数组大小 n = 9
最坏情况时间复杂度: O(n!) = 362880 次尝试
平均情况时间复杂度: O(n * n!)
----------------------------------
已尝试 10000 次 | 当前时间复杂度: O(10000) | 已运行时间: 1 ms
已尝试 20000 次 | 当前时间复杂度: O(20000) | 已运行时间: 3 ms
已尝试 30000 次 | 当前时间复杂度: O(30000) | 已运行时间: 4 ms
已尝试 40000 次 | 当前时间复杂度: O(40000) | 已运行时间: 6 ms
已尝试 50000 次 | 当前时间复杂度: O(50000) | 已运行时间: 8 ms
已尝试 60000 次 | 当前时间复杂度: O(60000) | 已运行时间: 9 ms
已尝试 70000 次 | 当前时间复杂度: O(70000) | 已运行时间: 11 ms
----------------------------------
排序完成!
实际尝试次数: 74861
理论最坏情况尝试次数: O(n!) = 362880
总运行时间: 12 ms
排序后: 1 1 3 4 4 5 9 76 346