Last active
January 10, 2017 06:15
-
-
Save h-otter/efadda2937b7beaf8f0bbb045a24d5b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <iterator> | |
#include <random> | |
#define N 100 | |
template <typename RandomAccessIterator, typename Predicate> | |
int bogo_sort(RandomAccessIterator begin, RandomAccessIterator end, Predicate p) { | |
std::random_device rd; | |
std::mt19937 generator(rd()); | |
int count = 0; | |
while (!std::is_sorted(begin, end, p)) { | |
std::shuffle(begin, end, generator); | |
count++; | |
} | |
return count; | |
} | |
template <typename RandomAccessIterator> | |
int bogo_sort(RandomAccessIterator begin, RandomAccessIterator end) { | |
return bogo_sort( | |
begin, end, | |
std::less< | |
typename std::iterator_traits<RandomAccessIterator>::value_type>()); | |
} | |
int main() { | |
int sum_count = 0; | |
double sum_time = 0; | |
for (int i = 0; i < N; i++){ | |
std::cout << "[*] " << i << " times" << std::endl; | |
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199}; | |
clock_t c1 = clock(); | |
int count = bogo_sort(std::begin(a), std::end(a)); | |
double this_time = (double)(clock() - c1) / CLOCKS_PER_SEC; | |
std::cout << "[*] sorted = "; | |
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); | |
std::cout << std::endl; | |
std::cout << "[*] count = " << count << ", time = " << this_time << std::endl; | |
sum_count += count; | |
sum_time += this_time; | |
std::cerr << "Please Enter Any Charactors: "; | |
char temp; | |
std::cin >> temp; | |
} | |
std::cout << "[+] RESULT: average count: " << sum_count / N << ", average time: " << sum_time / N << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment