Skip to content

Commit b01c2cf

Browse files
committed
Refactored bogo sort and bubble sort
1 parent 65361a4 commit b01c2cf

File tree

5 files changed

+127
-142
lines changed

5 files changed

+127
-142
lines changed

Sorts/BogoSort.java

-68
This file was deleted.

Sorts/BubbleSort.java

-70
This file was deleted.

Sorts/src/sort/BinaryTreeSort.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
import static sort.SortUtils.less;
44
import static sort.SortUtils.print;
55

6-
7-
public class BinaryTreeSort{
6+
/**
7+
*
8+
* @author Podshivalov Nikita (https://github.com/nikitap492)
9+
*
10+
* @see SortAlgorithm
11+
*
12+
*/
13+
public class BinaryTreeSort implements SortAlgorithm{
814

915
interface TreeVisitor<T extends Comparable<T>> {
1016
void visit(Node<T> node);
@@ -58,7 +64,8 @@ void traverse(TreeVisitor<T> visitor) {
5864
}
5965

6066

61-
private <T extends Comparable<T>> T[] sort(T[] array) {
67+
@Override
68+
public <T extends Comparable<T>> T[] sort(T[] array) {
6269

6370
Node<T> root = new Node<>(array[0]);
6471
for (int i = 1; i < array.length; i++) {
@@ -68,7 +75,6 @@ private <T extends Comparable<T>> T[] sort(T[] array) {
6875
root.traverse(new SortVisitor<>(array));
6976

7077
return array;
71-
7278
}
7379

7480

Sorts/src/sort/BogoSort.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sort;
2+
3+
import java.util.Random;
4+
5+
import static sort.SortUtils.*;
6+
7+
8+
/**
9+
*
10+
* @author Podshivalov Nikita (https://github.com/nikitap492)
11+
*
12+
* @see SortAlgorithm
13+
*
14+
*/
15+
public class BogoSort implements SortAlgorithm{
16+
17+
private static final Random random = new Random();
18+
19+
20+
private static <T extends Comparable<T>> boolean isSorted(T array[]){
21+
for(int i = 0; i<array.length - 1; i++){
22+
if(less(array[i + 1], array[i])) return false;
23+
}
24+
return true;
25+
}
26+
27+
// Randomly shuffles the array
28+
private static <T> void nextPermutation(T array[]){
29+
int length = array.length;
30+
31+
for (int i = 0; i < array.length; i++) {
32+
int randomIndex = i + random.nextInt(length - i);
33+
swap(array, randomIndex, i);
34+
}
35+
}
36+
37+
public <T extends Comparable<T>> T[] sort(T array[]) {
38+
while(!isSorted(array)){
39+
nextPermutation(array);
40+
}
41+
return array;
42+
}
43+
44+
// Driver Program
45+
public static void main(String[] args) {
46+
// Integer Input
47+
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
48+
49+
BogoSort bogoSort = new BogoSort();
50+
51+
// print a sorted array
52+
print(bogoSort.sort(integers));
53+
54+
// String Input
55+
String[] strings = {"c", "a", "e", "b","d"};
56+
57+
print(bogoSort.sort(strings));
58+
}
59+
}

Sorts/src/sort/BubbleSort.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package sort;
2+
3+
import static sort.SortUtils.print;
4+
import static sort.SortUtils.swap;
5+
6+
/**
7+
*
8+
* @author Varun Upadhyay (https://github.com/varunu28)
9+
* @author Podshivalov Nikita (https://github.com/nikitap492)
10+
*
11+
* @see SortAlgorithm
12+
*/
13+
14+
class BubbleSort implements SortAlgorithm{
15+
/**
16+
* This method implements the Generic Bubble Sort
17+
*
18+
* @param array The array to be sorted
19+
* Sorts the array in increasing order
20+
**/
21+
22+
@Override
23+
public <T extends Comparable<T>> T[] sort(T array[]) {
24+
int last = array.length;
25+
//Sorting
26+
boolean swap;
27+
do {
28+
swap = false;
29+
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;
34+
}
35+
}
36+
last--;
37+
} while (swap);
38+
return array;
39+
}
40+
41+
// Driver Program
42+
public static void main(String[] args) {
43+
44+
// Integer Input
45+
Integer[] integers = {4,23,6,78,1,54,231,9,12};
46+
BubbleSort bubbleSort = new BubbleSort();
47+
bubbleSort.sort(integers);
48+
49+
// Output => 1 4 6 9 12 23 54 78 231
50+
print(integers);
51+
52+
// String Input
53+
String[] strings = {"c", "a", "e", "b","d"};
54+
//Output => a b c d e
55+
print(bubbleSort.sort(strings));
56+
57+
}
58+
}

0 commit comments

Comments
 (0)