TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
bogo_sort.cpp File Reference

Implementation of Bogosort algorithm More...

#include <iostream>
#include <algorithm>
#include <array>
#include <cassert>
#include <random>
Include dependency graph for bogo_sort.cpp:

Go to the source code of this file.

Namespaces

namespace  sorting
 for working with vectors
 

Functions

template<typename T , size_t N>
std::array< T, N > sorting::shuffle (std::array< T, N > arr)
 
template<typename T , size_t N>
std::array< T, N > sorting::randomized_bogosort (std::array< T, N > arr)
 
template<typename T , size_t N>
void show_array (const std::array< T, N > &arr)
 
void test ()
 
int main ()
 

Detailed Description

Implementation of Bogosort algorithm

In computer science, bogosort (also known as permutation sort, stupid sort, slowsort, shotgun sort, random sort, monkey sort, bobosort or shuffle sort) is a highly inefficient sorting algorithm based on the generate and test paradigm. Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input.Randomized version is implemented here.

Algorithm

Shuffle the array untill array is sorted.

Author
Deep Raval

Definition in file bogo_sort.cpp.

Function Documentation

◆ main()

int main ( void )

Driver Code

Definition at line 107 of file bogo_sort.cpp.

107 {
108 // Testing
109 test();
110 // Example Usage
111 std::array <int, 5> arr = {3, 7, 10, 4, 1}; // Defining array which we want to sort
112 std::cout << "Original Array : ";
113 show_array(arr);
114 arr = sorting::randomized_bogosort(arr); // Callling bogo sort on it
115 std::cout << "Sorted Array : ";
116 show_array(arr); // Printing sorted array
117 return 0;
118}
void test()
Definition bogo_sort.cpp:81
void show_array(const std::array< T, N > &arr)
Definition bogo_sort.cpp:71
std::array< T, N > randomized_bogosort(std::array< T, N > arr)
Definition bogo_sort.cpp:52

◆ show_array()

template<typename T , size_t N>
void show_array ( const std::array< T, N > & arr)

Function to display array on screen

Template Parameters
Ttypename of the array
Nlength of array
Parameters
arrarray to display

Definition at line 71 of file bogo_sort.cpp.

71 {
72 for (int x : arr) {
73 std::cout << x << ' ';
74 }
75 std::cout << '\n';
76}

◆ test()

void test ( )

Function to test above algorithm

Definition at line 81 of file bogo_sort.cpp.

81 {
82 // Test 1
83 std::array <int, 5> arr1;
84 for (int &x : arr1) {
85 x = std::rand() % 100;
86 }
87 std::cout << "Original Array : ";
88 show_array(arr1);
90 std::cout << "Sorted Array : ";
91 show_array(arr1);
92 assert(std::is_sorted(arr1.begin(), arr1.end()));
93 // Test 2
94 std::array <int, 5> arr2;
95 for (int &x : arr2) {
96 x = std::rand() % 100;
97 }
98 std::cout << "Original Array : ";
99 show_array(arr2);
100 arr2 = sorting::randomized_bogosort(arr2);
101 std::cout << "Sorted Array : ";
102 show_array(arr2);
103 assert(std::is_sorted(arr2.begin(), arr2.end()));
104}