class Solution{ //Function to swap two elements in the array. public static void swap(int arr[], int x, int y){ //Use of a temporary variable to swap the elements. int tmp = arr[x]; arr[x] = arr[y]; arr[y] = tmp; } //Function to sort the array into a wave-like array. public static void convertToWave(int arr[], int n){ //Iterating over the array. for(int i=0;i<n-1;i=i+2){ //Swapping two neighbouring elements at a time. swap(arr, i, i+1); } return; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter