Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created August 28, 2015 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save viveksyngh/36f3bfb753e91310d369 to your computer and use it in GitHub Desktop.
Save viveksyngh/36f3bfb753e91310d369 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