Skip to content

Instantly share code, notes, and snippets.

@h-otter
Last active December 20, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-otter/a17b3918f389f72e71c50cf18e0bcce8 to your computer and use it in GitHub Desktop.
Save h-otter/a17b3918f389f72e71c50cf18e0bcce8 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <stdio.h>
#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