Skip to content

Commit 3676594

Browse files
committed
commit
1 parent 8e096b3 commit 3676594

File tree

2 files changed

+113
-34
lines changed

2 files changed

+113
-34
lines changed

CocktailSort.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Java program for implementation of Cocktail Sort
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+
// reset the swapped flag on entering the
13+
// loop, because it might be true from a
14+
// previous iteration.
15+
swapped = false;
16+
17+
// loop from bottom to top same as
18+
// the bubble sort.
19+
for (int i = start; i < end - 1; ++i)
20+
{
21+
if (a[i] > a[i + 1]) {
22+
int temp = a[i];
23+
a[i] = a[i + 1];
24+
a[i + 1] = temp;
25+
swapped = true;
26+
}
27+
}
28+
29+
// if nothing moved, then array is sorted.
30+
if (swapped == false)
31+
break;
32+
33+
// otherwise, reset the swapped flag so that it
34+
// can be used in the next stage
35+
swapped = false;
36+
37+
// move the end point back by one, because
38+
// item at the end is in its rightful spot
39+
end = end - 1;
40+
41+
// from top to bottom, doing the
42+
// same comparison as in the previous stage
43+
for (int i = end - 1; i >= start; i--)
44+
{
45+
if (a[i] > a[i + 1])
46+
{
47+
int temp = a[i];
48+
a[i] = a[i + 1];
49+
a[i + 1] = temp;
50+
swapped = true;
51+
}
52+
}
53+
54+
// increase the starting point, because
55+
// the last stage would have moved the next
56+
// smallest number to its rightful spot.
57+
start = start + 1;
58+
}
59+
}
60+
61+
/* Prints the array */
62+
void printArray(int a[])
63+
{
64+
int n = a.length;
65+
for (int i = 0; i < n; i++)
66+
System.out.print(a[i] + " ");
67+
System.out.println();
68+
}
69+
70+
// Driver code
71+
public static void main(String[] args)
72+
{
73+
CocktailSort ob = new CocktailSort();
74+
int a[] = { 5, 1, 4, 2, 8, 0, 2 };
75+
ob.cocktailSort(a);
76+
System.out.println("Sorted array");
77+
ob.printArray(a);
78+
}
79+
}

bubblesort.java

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
public class BubbleSortExample {
2-
static void bubbleSort(int[] arr) {
3-
int n = arr.length;
4-
int temp = 0;
5-
for(int i=0; i < n; i++){
6-
for(int j=1; j < (n-i); j++){
7-
if(arr[j-1] > arr[j]){
8-
//swap elements
9-
temp = arr[j-1];
10-
arr[j-1] = arr[j];
11-
arr[j] = temp;
12-
}
13-
14-
}
15-
}
16-
17-
}
18-
public static void main(String[] args) {
19-
int arr[] ={3,60,35,2,45,320,5};
20-
21-
System.out.println("Array Before Bubble Sort");
22-
for(int i=0; i < arr.length; i++){
23-
System.out.print(arr[i] + " ");
24-
}
25-
System.out.println();
26-
27-
bubbleSort(arr);//sorting array elements using bubble sort
28-
29-
System.out.println("Array After Bubble Sort");
30-
for(int i=0; i < arr.length; i++){
31-
System.out.print(arr[i] + " ");
32-
}
33-
34-
}
1+
public class BubbleSortExample {
2+
static void bubbleSort(int[] arr) {
3+
int n = arr.length;
4+
int temp = 0;
5+
for(int i=0; i < n; i++){
6+
for(int j=1; j < (n-i); j++){
7+
if(arr[j-1] > arr[j]){
8+
//swap elements
9+
temp = arr[j-1];
10+
arr[j-1] = arr[j];
11+
arr[j] = temp;
12+
}
13+
14+
}
15+
}
16+
17+
}
18+
public static void main(String[] args) {
19+
int arr[] ={3,60,35,2,45,320,5};
20+
21+
System.out.println("Array Before Bubble Sort");
22+
for(int i=0; i < arr.length; i++){
23+
System.out.print(arr[i] + " ");
24+
}
25+
System.out.println();
26+
27+
bubbleSort(arr);//sorting array elements using bubble sort
28+
29+
System.out.println("Array After Bubble Sort");
30+
for(int i=0; i < arr.length; i++){
31+
System.out.print(arr[i] + " ");
32+
}
33+
34+
}
3535
}

0 commit comments

Comments
 (0)