|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of the [Wave |
| 4 | + * sort](https://www.geeksforgeeks.org/sort-array-wave-form-2/) algorithm |
| 5 | + * @details |
| 6 | + * Wave Sort is a sorting algorithm that works in \f$O(nlogn)\f$ time assuming |
| 7 | + * the sort function used works in \f$O(nlogn)\f$ time. |
| 8 | + * @author [Swastika Gupta](https://github.com/Swastyy) |
| 9 | + */ |
| 10 | + |
| 11 | +#include <algorithm> /// for std::is_sorted, std::swap |
| 12 | +#include <cassert> /// for assert |
| 13 | +#include <iostream> /// for IO operations |
| 14 | +#include <vector> /// for std::vector |
| 15 | + |
| 16 | +/** |
| 17 | + * @namespace sorting |
| 18 | + * @brief Sorting algorithms |
| 19 | + */ |
| 20 | +namespace sorting { |
| 21 | +/** |
| 22 | + * @namespace wave_sort |
| 23 | + * @brief Functions for the [Wave |
| 24 | + * sort](https://www.geeksforgeeks.org/sort-array-wave-form-2/) implementation |
| 25 | + */ |
| 26 | +namespace wave_sort { |
| 27 | +/** |
| 28 | + * @brief The main function implements that implements the Wave Sort algorithm |
| 29 | + * @tparam T type of array |
| 30 | + * @param in_arr array to be sorted |
| 31 | + * @returns arr the wave sorted array |
| 32 | + */ |
| 33 | +template <typename T> |
| 34 | +std::vector<T> waveSort(const std::vector<T> &in_arr, int64_t n) { |
| 35 | + std::vector<T> arr(in_arr); |
| 36 | + |
| 37 | + for (int64_t i = 0; i < n; i++) { |
| 38 | + arr[i] = in_arr[i]; |
| 39 | + } |
| 40 | + std::sort(arr.begin(), arr.end()); |
| 41 | + for (int64_t i = 0; i < n - 1; i += 2) { // swap all the adjacent elements |
| 42 | + std::swap(arr[i], arr[i + 1]); |
| 43 | + } |
| 44 | + return arr; |
| 45 | +} |
| 46 | +} // namespace wave_sort |
| 47 | +} // namespace sorting |
| 48 | + |
| 49 | +/** |
| 50 | + * @brief Self-test implementations |
| 51 | + * @returns void |
| 52 | + */ |
| 53 | +static void test() { |
| 54 | + // [10, 90, 49, 2, 1, 5, 23] return [2, 1, 10, 5, 49, 23, 90] |
| 55 | + std::vector<int64_t> array1 = {10, 90, 49, 2, 1, 5, 23}; |
| 56 | + std::cout << "Test 1... "; |
| 57 | + std::vector<int64_t> arr1 = sorting::wave_sort::waveSort(array1, 7); |
| 58 | + const std::vector<int64_t> o1 = {2, 1, 10, 5, 49, 23, 90}; |
| 59 | + assert(arr1 == o1); |
| 60 | + std::cout << "passed" << std::endl; |
| 61 | + |
| 62 | + // [1, 3, 4, 2, 7, 8] return [2, 1, 4, 3, 8, 7] |
| 63 | + std::vector<int64_t> array2 = {1, 3, 4, 2, 7, 8}; |
| 64 | + std::cout << "Test 2... "; |
| 65 | + std::vector<int64_t> arr2 = sorting::wave_sort::waveSort(array2, 6); |
| 66 | + const std::vector<int64_t> o2 = {2, 1, 4, 3, 8, 7}; |
| 67 | + assert(arr2 == o2); |
| 68 | + std::cout << "passed" << std::endl; |
| 69 | + |
| 70 | + // [3, 3, 3, 3] return [3, 3, 3, 3] |
| 71 | + std::vector<int64_t> array3 = {3, 3, 3, 3}; |
| 72 | + std::cout << "Test 3... "; |
| 73 | + std::vector<int64_t> arr3 = sorting::wave_sort::waveSort(array3, 4); |
| 74 | + const std::vector<int64_t> o3 = {3, 3, 3, 3}; |
| 75 | + assert(arr3 == o3); |
| 76 | + std::cout << "passed" << std::endl; |
| 77 | + |
| 78 | + // [9, 4, 6, 8, 14, 3] return [4, 3, 8, 6, 14, 9] |
| 79 | + std::vector<int64_t> array4 = {9, 4, 6, 8, 14, 3}; |
| 80 | + std::cout << "Test 4... "; |
| 81 | + std::vector<int64_t> arr4 = sorting::wave_sort::waveSort(array4, 6); |
| 82 | + const std::vector<int64_t> o4 = {4, 3, 8, 6, 14, 9}; |
| 83 | + assert(arr4 == o4); |
| 84 | + std::cout << "passed" << std::endl; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * @brief Main function |
| 89 | + * @returns 0 on exit |
| 90 | + */ |
| 91 | +int main() { |
| 92 | + test(); // run self-test implementations |
| 93 | + return 0; |
| 94 | +} |
0 commit comments