- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathwave_sort.java
More file actions
Latest commit
23 lines (18 loc) · 589 Bytes
/
Copy pathwave_sort.java
File metadata and controls
23 lines (18 loc) · 589 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
importjava.util.Arrays;
publicclasswave_sort {
publicstaticvoidwave_sort(int[] arr) {
// Sorting the array in ascending order
Arrays.sort(arr);
// Swapping the elements in pairs
for (inti = 0; i < arr.length - 1; i += 2) {
inttemp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
publicstaticvoidmain(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
waveSort(arr);
System.out.println(Arrays.toString(arr)); // [2, 1, 4, 3, 6, 5, 8, 7, 9]
}
}