Skip to content

Commit 67e49a6

Browse files
Merge pull request #136 from Ragvendra1Rathore2/patch-3
Hacktoberfest-2022
2 parents fbe3e75 + 803daa3 commit 67e49a6

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

CocktailSort.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
public class CocktailSort
3+
{
4+
void cocktailSort(int a[])
5+
{
6+
boolean swapped = true;
7+
int start = 0;
8+
int end = a.length;
9+
10+
while (swapped == true)
11+
{
12+
13+
swapped = false;
14+
15+
for (int i = start; i < end - 1; ++i)
16+
{
17+
if (a[i] > a[i + 1]) {
18+
int temp = a[i];
19+
a[i] = a[i + 1];
20+
a[i + 1] = temp;
21+
swapped = true;
22+
}
23+
}
24+
25+
26+
if (swapped == false)
27+
break;
28+
29+
30+
swapped = false;
31+
32+
33+
end = end - 1;
34+
35+
36+
for (int i = end - 1; i >= start; i--)
37+
{
38+
if (a[i] > a[i + 1])
39+
{
40+
int temp = a[i];
41+
a[i] = a[i + 1];
42+
a[i + 1] = temp;
43+
swapped = true;
44+
}
45+
}
46+
47+
48+
start = start + 1;
49+
}
50+
}
51+
52+
53+
void printArray(int a[])
54+
{
55+
int n = a.length;
56+
for (int i = 0; i < n; i++)
57+
System.out.print(a[i] + " ");
58+
System.out.println();
59+
}
60+
61+
public static void main(String[] args)
62+
{
63+
CocktailSort ob = new CocktailSort();
64+
int a[] = { 5, 1, 4, 2, 8, 0, 2 };
65+
ob.cocktailSort(a);
66+
System.out.println("Sorted array");
67+
ob.printArray(a);
68+
}
69+
}

0 commit comments

Comments
 (0)