Skip to content

Commit 52d1182

Browse files
committed
Refactored BubbleSort, CycleSort, CocktailShakerSort
1 parent b01c2cf commit 52d1182

5 files changed

Lines changed: 157 additions & 94 deletions

File tree

Sorts/CocktailShakerSort.java

Lines changed: 0 additions & 86 deletions
This file was deleted.

Sorts/src/sort/BubbleSort.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sort;
22

3-
import static sort.SortUtils.print;
4-
import static sort.SortUtils.swap;
3+
import static sort.SortUtils.*;
54

65
/**
76
*
@@ -27,10 +26,8 @@ public <T extends Comparable<T>> T[] sort(T array[]) {
2726
do {
2827
swap = false;
2928
for (int count = 0; count < last-1; count++) {
30-
int comp = array[count].compareTo(array[count + 1]);
31-
if (comp > 0) {
32-
swap(array, count, count + 1);
33-
swap = true;
29+
if (less(array[count], array[count + 1])) {
30+
swap = swap(array, count, count + 1);
3431
}
3532
}
3633
last--;
@@ -42,7 +39,7 @@ public <T extends Comparable<T>> T[] sort(T array[]) {
4239
public static void main(String[] args) {
4340

4441
// Integer Input
45-
Integer[] integers = {4,23,6,78,1,54,231,9,12};
42+
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
4643
BubbleSort bubbleSort = new BubbleSort();
4744
bubbleSort.sort(integers);
4845

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package sort;
2+
3+
import static sort.SortUtils.*;
4+
5+
/**
6+
*
7+
* @author Mateus Bizzo (https://github.com/MattBizzo)
8+
* @author Podshivalov Nikita (https://github.com/nikitap492)
9+
*
10+
*/
11+
12+
class CocktailShakerSort implements SortAlgorithm {
13+
14+
/**
15+
* This method implements the Generic Cocktail Shaker Sort
16+
*
17+
* @param array The array to be sorted
18+
* Sorts the array in increasing order
19+
**/
20+
21+
@Override
22+
public <T extends Comparable<T>> T[] sort(T[] array){
23+
24+
int last = array.length;
25+
26+
// Sorting
27+
boolean swap;
28+
do {
29+
swap = false;
30+
31+
//front
32+
for (int count = 0; count <= last - 2; count++) {
33+
if (less(array[count + 1], array[count])) {
34+
swap = swap(array, count, count + 1);
35+
}
36+
}
37+
//break if no swap occurred
38+
if (!swap) {
39+
break;
40+
}
41+
swap = false;
42+
43+
//back
44+
for (int count = last - 2; count >= 0; count--) {
45+
if (less(array[count + 1], array[count])) {
46+
swap = swap(array, count, count + 1);
47+
}
48+
}
49+
last--;
50+
//end
51+
} while (swap);
52+
return array;
53+
}
54+
55+
// Driver Program
56+
public static void main(String[] args) {
57+
// Integer Input
58+
Integer[] integers = { 4, 23, 6, 78, 1, 54, 231, 9, 12 };
59+
CocktailShakerSort shakerSort = new CocktailShakerSort();
60+
61+
// Output => 1 4 6 9 12 23 54 78 231
62+
print(shakerSort.sort(integers));
63+
64+
// String Input
65+
String[] strings = { "c", "a", "e", "b", "d" };
66+
print(shakerSort.sort(strings));
67+
}
68+
69+
70+
}

Sorts/src/sort/CycleSort.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package sort;
2+
3+
import static sort.SortUtils.less;
4+
import static sort.SortUtils.print;
5+
6+
/**
7+
* @author Podshivalov Nikita (https://github.com/nikitap492)
8+
*/
9+
class CycleSort implements SortAlgorithm {
10+
11+
12+
@Override
13+
public <T extends Comparable<T>> T[] sort(T[] arr) {
14+
int n = arr.length;
15+
16+
// traverse array elements
17+
for (int j = 0; j <= n - 2; j++) {
18+
// initialize item as starting point
19+
T item = arr[j];
20+
21+
// Find position where we put the item.
22+
int pos = j;
23+
for (int i = j + 1; i < n; i++)
24+
if (less(arr[i], item)) pos++;
25+
26+
// If item is already in correct position
27+
if (pos == j) continue;
28+
29+
// ignore all duplicate elements
30+
while (item.compareTo(arr[pos]) == 0)
31+
pos += 1;
32+
33+
// put the item to it's right position
34+
if (pos != j) {
35+
item = replace(arr, pos, item);
36+
}
37+
38+
// Rotate rest of the cycle
39+
while (pos != j) {
40+
pos = j;
41+
42+
// Find position where we put the element
43+
for (int i = j + 1; i < n; i++)
44+
if (less(arr[i], item)){
45+
pos += 1;
46+
}
47+
48+
49+
// ignore all duplicate elements
50+
while (item.compareTo(arr[pos]) == 0)
51+
pos += 1;
52+
53+
// put the item to it's right position
54+
if (item != arr[pos]) {
55+
item = replace(arr, pos, item);
56+
}
57+
}
58+
}
59+
60+
return arr;
61+
}
62+
63+
private <T extends Comparable<T>> T replace(T[] arr, int pos, T item){
64+
T temp = item;
65+
item = arr[pos];
66+
arr[pos] = temp;
67+
return item;
68+
}
69+
70+
71+
72+
public static void main(String[] args) {
73+
Integer arr[] = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 };
74+
CycleSort cycleSort = new CycleSort();
75+
cycleSort.sort(arr);
76+
77+
System.out.println("After sort : ");
78+
print(arr);
79+
}
80+
81+
}

Sorts/src/sort/SortUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ final class SortUtils {
1818
* @param idx index of the first element
1919
* @param idy index of the second element
2020
*/
21-
static <T> void swap(T[] array, int idx, int idy){
21+
static <T> boolean swap(T[] array, int idx, int idy){
2222
T swap = array[idx];
2323
array[idx] = array[idy];
2424
array[idy] = swap;
25+
return true;
2526
}
2627

2728

0 commit comments

Comments
 (0)