-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathCocktailSort.java
More file actions
43 lines (35 loc) · 1.13 KB
/
CocktailSort.java
File metadata and controls
43 lines (35 loc) · 1.13 KB
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
35
36
37
38
39
40
41
42
43
public class CocktailSort
{
public static void cocktailSort(Integer[] inputArrays,
Integer n)
{
Boolean swapped = Boolean.TRUE;
Integer start = 0;
Integer end = inputArrays.length;
while (swapped) {
swapped = Boolean.FALSE;
for (int i = start; i < end - 1; ++i) {
if (inputArrays[i] > inputArrays[i + 1]) {
int temp = inputArrays[i];
inputArrays[i] = inputArrays[i + 1];
inputArrays[i + 1] = temp;
swapped = Boolean.TRUE;
}
}
if (!swapped) {
break;
}
swapped = Boolean.FALSE;
end--;
for (int i = end - 1; i >= start; i--) {
if (inputArrays[i] > inputArrays[i + 1]) {
int temp = inputArrays[i];
inputArrays[i] = inputArrays[i + 1];
inputArrays[i + 1] = temp;
swapped = Boolean.TRUE;
}
}
start++;
}
}
}