fork download
  1. // BogoSort.cpp : アプリケーションのエントリ ポイントを定義します。
  2. //
  3. #include <iostream>
  4. #include <random>
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. typedef std::vector<int> DType;
  9.  
  10. DType MakeVector(std::size_t N) {
  11. DType R;
  12. for (std::size_t i = 0; i < N; i++)R.push_back(i);
  13. return R;
  14. }
  15. template<class Iter,class RE>
  16. bool BogoSort(Iter F, Iter E, RE& r) {
  17. while (!std::is_sorted(F,E))std::shuffle(F, E, r);
  18.  
  19. return true;
  20. }
  21.  
  22. bool Show(const DType& v) {
  23. for (auto&o : v) {
  24. std::cout << o << ',';
  25. }
  26. std::cout << std::endl;
  27. return true;
  28. }
  29.  
  30. int main()
  31. {
  32. DType v=MakeVector(9);
  33. std::random_device rd;
  34. std::mt19937 mt;
  35.  
  36. std::shuffle(v.begin(), v.end(), mt);
  37. Show(v);
  38. BogoSort(v.begin(), v.end(),mt);
  39. Show(v);
  40. return 0;
  41. }
  42.  
  43.  
Success #stdin #stdout 0.02s 4516KB
stdin
Standard input is empty
stdout
5,8,0,3,4,2,6,7,1,
0,1,2,3,4,5,6,7,8,