human
What is a code to sort an array in wave form and the other steps required given the following:
Sort an array in wave form | Python function to sort the array arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] >= arr [ 5 ].
Traverse all even elements.
If current even element is smaller than previous.
If current even element is smaller than next.
Driver program"
How can the above be solved in Python?
assistant
#Here is code to sort an array in wave form and the other steps required.
 def sortInWave ( arr , n ) :
	for i in range ( 0 , n , 2 ) :
		if ( i > 0 and arr [ i ] < arr [ i - 1 ] ) :
			arr [ i ] , arr [ i - 1 ] = arr [ i - 1 ] , arr [ i ]
		if ( i < n - 1 and arr [ i ] < arr [ i + 1 ] ) :
			arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ]
arr = [ 10 , 90 , 49 , 2 , 1 , 5 , 23 ]
sortInWave ( arr , len ( arr ) )
for i in range ( 0 , len ( arr ) ) :
	print arr [ i ] ,