MCPcopy Create free account
hub / github.com/Rishabh062/Hacktoberfest2021 / cocktailSort

Method cocktailSort

Java/CocktailSort.java:4–59  ·  view source on GitHub ↗
(int a[])

Source from the content-addressed store, hash-verified

2public 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 */

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected