mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 02:25:57 +08:00
feat: add Bogo Sort (#952)
* Added Bogo Sort * Changed code according to guidelines * Added Comments and example * Improved code quality * Added range based loop and included cassert * Changed Vectors to Array * Added const to vector arg and now returning sorted array * Changed vector to array and changed description format * Added namespace sorting, Function description and tests * Update sorting/Bogo_Sort.cpp Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * Replaced Shuffle with std::random_shuffle * Renamed filename to smallercase, Added shuffle for ref and updated bracket styling * Added missing ')' * updating DIRECTORY.md * Added spaces in single line comments * Added Spaces * Update sorting/bogo_sort.cpp Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
@@ -215,6 +215,7 @@
|
||||
## Sorting
|
||||
* [Bead Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bead_sort.cpp)
|
||||
* [Bitonic Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bitonic_sort.cpp)
|
||||
* [Bogo Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bogo_sort.cpp)
|
||||
* [Bubble Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bubble_sort.cpp)
|
||||
* [Bucket Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bucket_sort.cpp)
|
||||
* [Cocktail Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/cocktail_selection_sort.cpp)
|
||||
|
||||
115
sorting/bogo_sort.cpp
Normal file
115
sorting/bogo_sort.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of [Bogosort algorithm](https://en.wikipedia.org/wiki/Bogosort)
|
||||
*
|
||||
* @details
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
/**
|
||||
* @namespace sorting
|
||||
* @brief Sorting algorithms
|
||||
*/
|
||||
namespace sorting {
|
||||
/**
|
||||
* Function to shuffle the elements of an array. (for reference)
|
||||
* @tparam T typename of the array
|
||||
* @tparam N length of array
|
||||
* @param arr array to shuffle
|
||||
* @returns new array with elements shuffled from a given array
|
||||
*/
|
||||
template <typename T, size_t N>
|
||||
std::array <T, N> shuffle (std::array <T, N> arr) {
|
||||
for (int i = 0; i < N; i++) {
|
||||
// Swaps i'th index with random index (less than array size)
|
||||
std::swap(arr[i], arr[std::rand() % N]);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
/**
|
||||
* Implement randomized Bogosort algorithm and sort the elements of a given array.
|
||||
* @tparam T typename of the array
|
||||
* @tparam N length of array
|
||||
* @param arr array to sort
|
||||
* @returns new array with elements sorted from a given array
|
||||
*/
|
||||
template <typename T, size_t N>
|
||||
std::array <T, N> randomized_bogosort (std::array <T, N> arr) {
|
||||
// Untill array is not sorted
|
||||
while (!std::is_sorted(arr.begin(), arr.end())) {
|
||||
std::random_shuffle(arr.begin(), arr.end());// Shuffle the array
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
} // namespace sorting
|
||||
|
||||
/**
|
||||
* Function to display array on screen
|
||||
* @tparam T typename of the array
|
||||
* @tparam N length of array
|
||||
* @param arr array to display
|
||||
*/
|
||||
template <typename T, size_t N>
|
||||
void show_array (const std::array <T, N> &arr) {
|
||||
for (int x : arr) {
|
||||
std::cout << x << ' ';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to test above algorithm
|
||||
*/
|
||||
void test() {
|
||||
// Test 1
|
||||
std::array <int, 5> arr1;
|
||||
for (int &x : arr1) {
|
||||
x = std::rand() % 100;
|
||||
}
|
||||
std::cout << "Original Array : ";
|
||||
show_array(arr1);
|
||||
arr1 = sorting::randomized_bogosort(arr1);
|
||||
std::cout << "Sorted Array : ";
|
||||
show_array(arr1);
|
||||
assert(std::is_sorted(arr1.begin(), arr1.end()));
|
||||
// Test 2
|
||||
std::array <int, 5> arr2;
|
||||
for (int &x : arr2) {
|
||||
x = std::rand() % 100;
|
||||
}
|
||||
std::cout << "Original Array : ";
|
||||
show_array(arr2);
|
||||
arr2 = sorting::randomized_bogosort(arr2);
|
||||
std::cout << "Sorted Array : ";
|
||||
show_array(arr2);
|
||||
assert(std::is_sorted(arr2.begin(), arr2.end()));
|
||||
}
|
||||
|
||||
/** Driver Code */
|
||||
int main() {
|
||||
// Testing
|
||||
test();
|
||||
// Example Usage
|
||||
std::array <int, 5> arr = {3, 7, 10, 4, 1}; // Defining array which we want to sort
|
||||
std::cout << "Original Array : ";
|
||||
show_array(arr);
|
||||
arr = sorting::randomized_bogosort(arr); // Callling bogo sort on it
|
||||
std::cout << "Sorted Array : ";
|
||||
show_array(arr); // Printing sorted array
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user