From 7bab516e59c10a2c51e5a24a55b9c7e20442115d Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Sat, 17 Jul 2021 00:11:54 +0530 Subject: [PATCH] feat: Add the Wave Sort algorithm (#1525) * Create wave_sort.cpp * Update wave_sort.cpp * Update wave_sort.cpp * Update wave_sort.cpp * updating DIRECTORY.md * clang-format and clang-tidy fixes for 196165e7 * Update wave_sort.cpp * Update sorting/wave_sort.cpp Co-authored-by: David Leal * Update sorting/wave_sort.cpp Co-authored-by: David Leal * Update sorting/wave_sort.cpp Co-authored-by: David Leal * Update wave_sort.cpp * clang-format and clang-tidy fixes for b5f9663f * Update sorting/wave_sort.cpp Co-authored-by: David Leal * Update sorting/wave_sort.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for 8651884e Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal --- DIRECTORY.md | 1 + sorting/wave_sort.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 sorting/wave_sort.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index f56b46e2e..00757255a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -311,6 +311,7 @@ * [Strand Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/strand_sort.cpp) * [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp) * [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/tim_sort.cpp) + * [Wave Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/wave_sort.cpp) * [Wiggle Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/wiggle_sort.cpp) ## Strings diff --git a/sorting/wave_sort.cpp b/sorting/wave_sort.cpp new file mode 100644 index 000000000..85fdb61ad --- /dev/null +++ b/sorting/wave_sort.cpp @@ -0,0 +1,94 @@ +/** + * @file + * @brief Implementation of the [Wave + * sort](https://www.geeksforgeeks.org/sort-array-wave-form-2/) algorithm + * @details + * Wave Sort is a sorting algorithm that works in \f$O(nlogn)\f$ time assuming + * the sort function used works in \f$O(nlogn)\f$ time. + * @author [Swastika Gupta](https://github.com/Swastyy) + */ + +#include /// for std::is_sorted, std::swap +#include /// for assert +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace sorting + * @brief Sorting algorithms + */ +namespace sorting { +/** + * @namespace wave_sort + * @brief Functions for the [Wave + * sort](https://www.geeksforgeeks.org/sort-array-wave-form-2/) implementation + */ +namespace wave_sort { +/** + * @brief The main function implements that implements the Wave Sort algorithm + * @tparam T type of array + * @param in_arr array to be sorted + * @returns arr the wave sorted array + */ +template +std::vector waveSort(const std::vector &in_arr, int64_t n) { + std::vector arr(in_arr); + + for (int64_t i = 0; i < n; i++) { + arr[i] = in_arr[i]; + } + std::sort(arr.begin(), arr.end()); + for (int64_t i = 0; i < n - 1; i += 2) { // swap all the adjacent elements + std::swap(arr[i], arr[i + 1]); + } + return arr; +} +} // namespace wave_sort +} // namespace sorting + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + // [10, 90, 49, 2, 1, 5, 23] return [2, 1, 10, 5, 49, 23, 90] + std::vector array1 = {10, 90, 49, 2, 1, 5, 23}; + std::cout << "Test 1... "; + std::vector arr1 = sorting::wave_sort::waveSort(array1, 7); + const std::vector o1 = {2, 1, 10, 5, 49, 23, 90}; + assert(arr1 == o1); + std::cout << "passed" << std::endl; + + // [1, 3, 4, 2, 7, 8] return [2, 1, 4, 3, 8, 7] + std::vector array2 = {1, 3, 4, 2, 7, 8}; + std::cout << "Test 2... "; + std::vector arr2 = sorting::wave_sort::waveSort(array2, 6); + const std::vector o2 = {2, 1, 4, 3, 8, 7}; + assert(arr2 == o2); + std::cout << "passed" << std::endl; + + // [3, 3, 3, 3] return [3, 3, 3, 3] + std::vector array3 = {3, 3, 3, 3}; + std::cout << "Test 3... "; + std::vector arr3 = sorting::wave_sort::waveSort(array3, 4); + const std::vector o3 = {3, 3, 3, 3}; + assert(arr3 == o3); + std::cout << "passed" << std::endl; + + // [9, 4, 6, 8, 14, 3] return [4, 3, 8, 6, 14, 9] + std::vector array4 = {9, 4, 6, 8, 14, 3}; + std::cout << "Test 4... "; + std::vector arr4 = sorting::wave_sort::waveSort(array4, 6); + const std::vector o4 = {4, 3, 8, 6, 14, 9}; + assert(arr4 == o4); + std::cout << "passed" << std::endl; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +}