Loading...
Searching...
No Matches
[Wiggle Sort Algorithm] (https://leetcode.com/problems/wiggle-sort-ii/) Implementation More...
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <vector>
Go to the source code of this file.
template<typename T> std::vector< T > sorting::wiggle_sort::wiggleSort (const std::vector< T > &arr) Function used for sorting the elements in wave form.[Wiggle Sort Algorithm] (https://leetcode.com/problems/wiggle-sort-ii/) Implementation
Wiggle Sort sorts the array into a wave like array. An array ‘arr[0..n-1]’ is sorted in wave form, if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= …..
Definition in file wiggle_sort.cpp.
◆ wiggleSort()template<typename T>
std::vector< T > sorting::wiggle_sort::wiggleSort ( const std::vector< T > & arr )Function used for sorting the elements in wave form.
Checking whether the even indexed elements are greater than their adjacent odd elements. Traversing all even indexed elements of the input arr. If current element is smaller than the previous odd element, swap them. If current element is smaller than the next odd element, swap them.
Definition at line 54 of file wiggle_sort.cpp.
54 {
55 uint32_t size = arr.size();
56
57 std::vector<T> out(
58 arr);
59
60
61 for (int i = 0; i < size; i += 2) {
62 if (i > 0 && out[i - 1] > out[i]) {
63 std::swap(out[i], out[i - 1]);
64 }
65
66 if (i < size - 1 && out[i] < out[i + 1]) {
67 std::swap(out[i], out[i + 1]);
68 }
69 }
70
71 return out;
72}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4