Refactored bogo sort and bubble sort

This commit is contained in:
nik
2018-04-09 15:24:16 +03:00
parent 65361a4445
commit b01c2cf2c6
5 changed files with 127 additions and 142 deletions

View File

@@ -3,8 +3,14 @@ package sort;
import static sort.SortUtils.less;
import static sort.SortUtils.print;
public class BinaryTreeSort{
/**
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SortAlgorithm
*
*/
public class BinaryTreeSort implements SortAlgorithm{
interface TreeVisitor<T extends Comparable<T>> {
void visit(Node<T> node);
@@ -58,7 +64,8 @@ public class BinaryTreeSort{
}
private <T extends Comparable<T>> T[] sort(T[] array) {
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
Node<T> root = new Node<>(array[0]);
for (int i = 1; i < array.length; i++) {
@@ -68,7 +75,6 @@ public class BinaryTreeSort{
root.traverse(new SortVisitor<>(array));
return array;
}

View File

@@ -0,0 +1,59 @@
package sort;
import java.util.Random;
import static sort.SortUtils.*;
/**
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SortAlgorithm
*
*/
public class BogoSort implements SortAlgorithm{
private static final Random random = new Random();
private static <T extends Comparable<T>> boolean isSorted(T array[]){
for(int i = 0; i<array.length - 1; i++){
if(less(array[i + 1], array[i])) return false;
}
return true;
}
// Randomly shuffles the array
private static <T> void nextPermutation(T array[]){
int length = array.length;
for (int i = 0; i < array.length; i++) {
int randomIndex = i + random.nextInt(length - i);
swap(array, randomIndex, i);
}
}
public <T extends Comparable<T>> T[] sort(T array[]) {
while(!isSorted(array)){
nextPermutation(array);
}
return array;
}
// Driver Program
public static void main(String[] args) {
// Integer Input
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
BogoSort bogoSort = new BogoSort();
// print a sorted array
print(bogoSort.sort(integers));
// String Input
String[] strings = {"c", "a", "e", "b","d"};
print(bogoSort.sort(strings));
}
}

View File

@@ -0,0 +1,58 @@
package sort;
import static sort.SortUtils.print;
import static sort.SortUtils.swap;
/**
*
* @author Varun Upadhyay (https://github.com/varunu28)
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SortAlgorithm
*/
class BubbleSort implements SortAlgorithm{
/**
* This method implements the Generic Bubble Sort
*
* @param array The array to be sorted
* Sorts the array in increasing order
**/
@Override
public <T extends Comparable<T>> T[] sort(T array[]) {
int last = array.length;
//Sorting
boolean swap;
do {
swap = false;
for (int count = 0; count < last-1; count++) {
int comp = array[count].compareTo(array[count + 1]);
if (comp > 0) {
swap(array, count, count + 1);
swap = true;
}
}
last--;
} while (swap);
return array;
}
// Driver Program
public static void main(String[] args) {
// Integer Input
Integer[] integers = {4,23,6,78,1,54,231,9,12};
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.sort(integers);
// Output => 1 4 6 9 12 23 54 78 231
print(integers);
// String Input
String[] strings = {"c", "a", "e", "b","d"};
//Output => a b c d e
print(bubbleSort.sort(strings));
}
}