forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CocktailSort.java
106 lines (91 loc) · 2.81 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.*;
public class CocktailSort {
private static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static void cocktailSort(int arr[]) {
boolean isSwapped = true;
int begin = 0;
int last = arr.length-1;
while (isSwapped == true) {
// set isSwapped to false so that if nothing gets
// sorted in this iteration we can break right away
isSwapped = false;
for(int i = begin; i < last; i++) {
// going forward
if (arr[i] > arr[i+1]) {
swap(arr, i, i+1);
isSwapped = true;
}
}
// if nothing swapped then the array is already sorted
if (isSwapped == false)
break;
// else, reset isSwapped to check for next iteration
isSwapped = false;
// since largest item is now in last. we only need
// to check for one place before it
last -= 1;
for (int i = last; i >= begin; i--) {
// going backward
if (arr[i] > arr[i+1]) {
swap(arr, i, i+1);
isSwapped = true;
}
}
// now the smallest number is on first place
// so move starting point one position ahead
begin++;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//taking input array
System.out.println("Enter size of array:");
int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter array elements:");
for (int i=0; i<size; i++) {
arr[i] = sc.nextInt();
}
sc.close();
// before sorting
System.out.println("Array before Cocktail sort:");
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
cocktailSort(arr);
// after sorting
System.out.println("Array after Cocktail sort:");
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/**
* Sample input/output
* Enter size of array:
* 10
* Enter array elements:
* 88 26 77 49 91 62 33 85 99 11
* Array before Cocktail sort:
* 88 26 77 49 91 62 33 85 99 11
* Array after Cocktail sort:
* 11 26 33 49 62 77 85 88 91 99
*
* Enter size of array:
* 7
* Enter array elements:
* 5 1 4 2 8 0 2
* Array before Cocktail sort:
* 5 1 4 2 8 0 2
* Array after Cocktail sort:
* 0 1 2 2 4 5 8
*
* Worst case Time complexity = O(n*n)
* Best case Time complexity = O(n) when array is already sorted
* Auxillary space = O(1)
*/