Sort an array in wave form | Java implementation of naive method for sorting an array in wave form . ; A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] . . ; Sort the input array ; Swap adjacent elements ; Driver method
import java . util . * ; class SortWave { void swap ( int arr [ ] , int a , int b ) { int temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } void sortInWave ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i += 2 ) swap ( arr , i , i + 1 ) ; } public static void main ( String args [ ] ) { SortWave ob = new SortWave ( ) ; int arr [ ] = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = arr . length ; ob . sortInWave ( arr , n ) ; for ( int i : arr ) System . out . print ( i + " ▁ " ) ; } }