Skip to content

Commit f9a2893

Browse files
authored
Merge pull request deutranium#199 from K-u-n-a-l-c/patch-5
Create cocktailsort.java
2 parents 471b869 + e5a8789 commit f9a2893

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class cocktail{
2+
static int temp;
3+
static void Cocktail(int a[], int n){
4+
boolean swap = true;
5+
int begin = 0,i;
6+
int end = n - 1;
7+
while (swap) {
8+
swap = false;
9+
for (i = begin; i < end; ++i){
10+
if (a[i] > a[i + 1]){
11+
temp = a[i];
12+
a[i]=a[i+1];
13+
a[i+1]=temp;
14+
swap = true;
15+
}
16+
}
17+
if (!swap)
18+
break;
19+
swap = false;
20+
for (i = end - 1; i >= begin; --i){
21+
if (a[i] > a[i + 1]){
22+
temp = a[i];
23+
a[i]=a[i+1];
24+
a[i+1]=temp;
25+
swap = true;
26+
}
27+
}
28+
++begin;
29+
}
30+
}
31+
public static void main(String[] args) {
32+
int my_arr[] = {34, 78, 90, 32, 67, 12, 1, 0, 95};
33+
Cocktail(my_arr, my_arr.length);
34+
System.out.println("The sorted array is ");
35+
for (int i = 0; i < my_arr.length; i++)
36+
System.out.print(my_arr[i]+" ");
37+
System.out.println();
38+
}
39+
}

0 commit comments

Comments
 (0)