fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6.  
  7. using namespace std;
  8.  
  9. // 检查数组是否已排序
  10. bool isSorted(const vector<int>& arr) {
  11. for (size_t i = 0; i < arr.size() - 1; ++i) {
  12. if (arr[i] > arr[i + 1]) {
  13. return false;
  14. }
  15. }
  16. return true;
  17. }
  18.  
  19. // 猴子排序实现
  20. void bogoSort(vector<int>& arr) {
  21. // 使用时间作为随机种子
  22. unsigned seed = chrono::system_clock::now().time_since_epoch().count();
  23. default_random_engine engine(seed);
  24.  
  25. int attempts = 0; // 记录尝试次数
  26.  
  27. // 当数组未排序时继续打乱
  28. while (!isSorted(arr)) {
  29. // 随机打乱数组
  30. shuffle(arr.begin(), arr.end(), engine);
  31. attempts++;
  32.  
  33. // 每10000次尝试打印一次进度(可选)
  34. if (attempts % 10000 == 0) {
  35. cout << "已尝试 " << attempts << " 次..." << endl;
  36. }
  37. }
  38.  
  39. cout << "排序完成!总共尝试了 " << attempts << " 次。" << endl;
  40. }
  41.  
  42. int main() {
  43. vector<int> arr = {5, 2, 9, 1, 5, 6,5699,16,63,47};
  44.  
  45. cout << "排序前: ";
  46. for (int num : arr) {
  47. cout << num << " ";
  48. }
  49. cout << endl;
  50.  
  51. bogoSort(arr);
  52.  
  53. cout << "排序后: ";
  54. for (int num : arr) {
  55. cout << num << " ";
  56. }
  57. cout << endl;
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0.17s 5328KB
stdin
Standard input is empty
stdout
排序前: 5 2 9 1 5 6 5699 16 63 47 
已尝试 10000 次...
已尝试 20000 次...
已尝试 30000 次...
已尝试 40000 次...
已尝试 50000 次...
已尝试 60000 次...
已尝试 70000 次...
已尝试 80000 次...
已尝试 90000 次...
已尝试 100000 次...
已尝试 110000 次...
已尝试 120000 次...
已尝试 130000 次...
已尝试 140000 次...
已尝试 150000 次...
已尝试 160000 次...
已尝试 170000 次...
已尝试 180000 次...
已尝试 190000 次...
已尝试 200000 次...
已尝试 210000 次...
已尝试 220000 次...
已尝试 230000 次...
已尝试 240000 次...
已尝试 250000 次...
已尝试 260000 次...
已尝试 270000 次...
已尝试 280000 次...
已尝试 290000 次...
已尝试 300000 次...
已尝试 310000 次...
已尝试 320000 次...
已尝试 330000 次...
已尝试 340000 次...
已尝试 350000 次...
已尝试 360000 次...
已尝试 370000 次...
已尝试 380000 次...
已尝试 390000 次...
已尝试 400000 次...
已尝试 410000 次...
已尝试 420000 次...
已尝试 430000 次...
已尝试 440000 次...
已尝试 450000 次...
已尝试 460000 次...
已尝试 470000 次...
已尝试 480000 次...
已尝试 490000 次...
已尝试 500000 次...
已尝试 510000 次...
已尝试 520000 次...
已尝试 530000 次...
已尝试 540000 次...
已尝试 550000 次...
已尝试 560000 次...
已尝试 570000 次...
已尝试 580000 次...
已尝试 590000 次...
已尝试 600000 次...
已尝试 610000 次...
已尝试 620000 次...
已尝试 630000 次...
已尝试 640000 次...
已尝试 650000 次...
已尝试 660000 次...
已尝试 670000 次...
已尝试 680000 次...
已尝试 690000 次...
已尝试 700000 次...
已尝试 710000 次...
已尝试 720000 次...
已尝试 730000 次...
已尝试 740000 次...
已尝试 750000 次...
已尝试 760000 次...
已尝试 770000 次...
已尝试 780000 次...
已尝试 790000 次...
已尝试 800000 次...
已尝试 810000 次...
已尝试 820000 次...
已尝试 830000 次...
已尝试 840000 次...
已尝试 850000 次...
已尝试 860000 次...
已尝试 870000 次...
已尝试 880000 次...
已尝试 890000 次...
已尝试 900000 次...
已尝试 910000 次...
已尝试 920000 次...
已尝试 930000 次...
已尝试 940000 次...
排序完成!总共尝试了 943183 次。
排序后: 1 2 5 5 6 9 16 47 63 5699