Skip to content

Instantly share code, notes, and snippets.

@awaemmanuel
Forked from viveksyngh/WAVE.py
Created March 22, 2018 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awaemmanuel/1cf0ca2d241bc865ed162c1724d0d64d to your computer and use it in GitHub Desktop.
Save awaemmanuel/1cf0ca2d241bc865ed162c1724d0d64d to your computer and use it in GitHub Desktop.
Given an array of integers, sort the array into a wave like array and return it, In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5.....
# @param A : A List of integers
# @return List of integers
def wave(A):
A.sort()
i = 0
while i < len(A) :
if i + 1 < len(A) :
A[i], A[i+1] = A[i+1], A[i]
i = i + 2
else :
i = i + 1
return A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment