-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCocktailSort.java
52 lines (42 loc) · 1.3 KB
/
CocktailSort.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package sorting;
public class CocktailSort {
public static void main(String []args) {
int[] arr = {2,5,6,4,11,22,10,1};
System.out.println("Before Sorting:");
for(int j=0; j<arr.length; j++)
System.out.print(arr[j] + " ");
sort(arr);
System.out.println("\n\nAfter Sorting:");
for(int j=0; j<arr.length; j++)
System.out.print(arr[j] + " ");
}
public static void sort(int[] array) {
int start = -1;
int end = array.length - 2;
boolean swapped;
do {
swapped = false;
for (int i = ++start; i <= end; i++) {
if (array[i] > array[i + 1]) {
swap(array, i, i + 1);
swapped = true;
}
}
if (!swapped) {
break;
}
swapped = false;
for (int i = --end; i >= start; i--) {
if (array[i] > array[i + 1]) {
swap(array, i, i + 1);
swapped = true;
}
}
} while (swapped);
}
private static void swap(int[] array, int a, int b) {
Integer temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}