A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d1/dcc/wiggle__sort_8cpp.html below:

TheAlgorithms/C++: sorting/wiggle_sort.cpp File Reference

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.
template<typename T> static void  displayElements (const std::vector< T > &arr)   Utility function used for printing the elements. Prints elements of the array after they're sorted using wiggle sort algorithm.
static void  test () int  main ()

[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.

Parameters
arr input array (unsorted elements)
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/sorting/wiggle_sort.cpp.

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