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

Implementation of the Wave sort algorithm. More...

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for wave_sort.cpp:

Go to the source code of this file.

Namespaces

namespace  sorting
 for working with vectors
 
namespace  wave_sort
 Functions for the Wave sort implementation.
 

Functions

template<typename T >
std::vector< T > sorting::wave_sort::waveSort (const std::vector< T > &in_arr, int64_t n)
 The main function implements that implements the Wave Sort algorithm.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of the Wave sort algorithm.

Wave Sort is a sorting algorithm that works in \(O(nlogn)\) time assuming the sort function used works in \(O(nlogn)\) time.

Author
Swastika Gupta

Definition in file wave_sort.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 91 of file wave_sort.cpp.

91 {
92 test(); // run self-test implementations
93 return 0;
94}
static void test()
Self-test implementations.
Definition wave_sort.cpp:53

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 53 of file wave_sort.cpp.

53 {
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}
std::vector< T > waveSort(const std::vector< T > &in_arr, int64_t n)
The main function implements that implements the Wave Sort algorithm.
Definition wave_sort.cpp:34

◆ waveSort()

template<typename T >
std::vector< T > sorting::wave_sort::waveSort ( const std::vector< T > & in_arr,
int64_t n )

The main function implements that implements the Wave Sort algorithm.

Template Parameters
Ttype of array
Parameters
in_arrarray to be sorted
Returns
arr the wave sorted array

Definition at line 34 of file wave_sort.cpp.

34 {
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}