MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / sortInWave

Method sortInWave

Programs/WiggleSorting.java:15–28  ·  view source on GitHub ↗
(int[] arr, int n)

Source from the content-addressed store, hash-verified

13 // This function sorts arr[0..n-1] in wave form, i.e.,
14 // arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4]....
15 void sortInWave(int[] arr, int n) {
16 // Traverse all even elements
17 for (int i = 0; i < n; i += 2) {
18 // If current even element is smaller
19 // than previous
20 if (i > 0 && arr[i - 1] > arr[i])
21 swap(arr, i - 1, i);
22
23 // If current even element is smaller
24 // than next
25 if (i < n - 1 && arr[i] < arr[i + 1])
26 swap(arr, i, i + 1);
27 }
28 }
29}
30public class WiggleSorting {
31 public static void main(String[] args) {

Callers 1

mainMethod · 0.95

Calls 1

swapMethod · 0.95

Tested by

no test coverage detected