Sort Array Wave Form, is a Sorting and Searching related problem and in this post we will see how we can solve this challenge in C++

given an array.Sort it in wave form such that a[0] >= a[1] <=a[2] >= a[3].... can be done using:

  1. sort the array and swap the adjacent element after gap of 1 Tc:O(nlogn)
  2. we are only concerned with the even positions,where the element is supposed to be greater than preceding odd and next odd place element for all even positions: if curr < pred : swap(pred,curr) if curr < next swap(next, curr)

Please check the main.cpp snippet for the solution.

This solution originally posted at: Github by @susantabiswas