forked from hexlet-components/ru-my-first-pullrequest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShakerSort.java
29 lines (27 loc) · 871 Bytes
/
ShakerSort.java
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
// Шейкерная сортировка. Shaker sort
public class ShakerSort {
public static void sort(int[] array) {
if (array.length == 0 || array.length == 1) return; // Для пустых не работаем.
int left = 0;
int right = array.length - 1;
while (left <= right) {
for (int i = right; i > left; i--) {
if (array[i] > array[i - 1]) {
swap(array, i, i - 1);
}
}
left++;
for (int i = left; i < right; i++) {
if (array[i] < array[i + 1]) {
swap(array, i, i + 1);
}
}
right--;
}
}
private static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}