Wave Array Python

PROGRAM TO PRINT THE GIVEN UNSORTED ARRAY IN WAVE LIKE SORTED ARRAY.



def sortInWave(arr, n):
      
    # Traverse all even elements
    for i in range(0, n, 2):
          
        # If current even element is smaller than previous
        if (i> 0 and arr[i] < arr[i-1]):
            arr[i],arr[i-1] = arr[i-1],arr[i]
          
        # If current even element is smaller than next
        if (i < n-1 and arr[i] < arr[i+1]):
            arr[i],arr[i+1] = arr[i+1],arr[i]
  
# Driver program
arr = [10, 90, 49, 2, 1, 5, 23]
sortInWave(arr, len(arr))
for i in range(0,len(arr)):
    print arr[i],

OUTPUT
90 10 49 1 5 2 23

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java