forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaveSort.java
More file actions
34 lines (30 loc) · 991 Bytes
/
Copy pathWaveSort.java
File metadata and controls
34 lines (30 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.geeksforgeeks.array;
public class WaveSort {
public static void main(String[] args) {
int arr[] = {10, 5, 6, 3, 2, 20, 100, 80};
// int arr[] = {20, 10, 8, 6, 4, 2};
// int arr[] = {2, 4, 6, 8, 10, 20};
sort(arr);
ArrayRotation.printArray(arr);
}
public static void sort(int[] sample) {
boolean upTrend = false;
for (int j = 0; j < sample.length - 1; j++) {
if (!upTrend) {
if (sample[j] < sample[j + 1]) {
int temp = sample[j];
sample[j] = sample[j + 1];
sample[j + 1] = temp;
}
upTrend = !upTrend;
} else {
if (sample[j] > sample[j + 1]) {
int temp = sample[j];
sample[j] = sample[j + 1];
sample[j + 1] = temp;
}
upTrend = !upTrend;
}
}
}
}