diff --git a/Sorts/BogoSort.java b/Sorts/BogoSort.java deleted file mode 100644 index 33787178f..000000000 --- a/Sorts/BogoSort.java +++ /dev/null @@ -1,68 +0,0 @@ -package Sorts; - -import java.util.Random; - -public class BogoSort { - private static void swap(T array[], int first, int second){ - T randomElement = array[first]; - array[first] = array[second]; - array[second] = randomElement; - } - - private static > boolean isSorted(T array[]){ - for(int i = 0; i 0) return false; - } - return true; - } - - // Randomly shuffles the array - private static void nextPermutation(T array[]){ - int length = array.length; - Random random = new Random(); - - for (int i = 0; i < array.length; i++) { - int randomIndex = i + random.nextInt(length - i); - swap(array, randomIndex, i); - } - } - - public static > void bogoSort(T array[]) { - while(!isSorted(array)){ - nextPermutation(array); - } - } - - // Driver Program - public static void main(String[] args) - { - // Integer Input - int[] arr1 = {4,23,6,78,1,54,231,9,12}; - int last = arr1.length; - Integer[] array = new Integer[last]; - for (int i=0;i 1 4 6 9 12 23 54 78 231 - for(int i=0; i a b c d e - for(int i=0; i> void BS(T array[], int last) { - //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) - { - T temp = array[count]; - array[count] = array[count + 1]; - array[count + 1] = temp; - swap = true; - } - } - last--; - } while (swap); - } - - // Driver Program - public static void main(String[] args) - { - // Integer Input - int[] arr1 = {4,23,6,78,1,54,231,9,12}; - int last = arr1.length; - Integer[] array = new Integer[last]; - for (int i=0;i 1 4 6 9 12 23 54 78 231 - for(int i=0; i a b c d e - for(int i=0; i> { void visit(Node node); @@ -58,7 +64,8 @@ public class BinaryTreeSort{ } - private > T[] sort(T[] array) { + @Override + public > T[] sort(T[] array) { Node 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; - } diff --git a/Sorts/src/sort/BogoSort.java b/Sorts/src/sort/BogoSort.java new file mode 100644 index 000000000..b367df850 --- /dev/null +++ b/Sorts/src/sort/BogoSort.java @@ -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 > boolean isSorted(T array[]){ + for(int i = 0; i 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[] 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)); + } +} diff --git a/Sorts/src/sort/BubbleSort.java b/Sorts/src/sort/BubbleSort.java new file mode 100644 index 000000000..67782cf0d --- /dev/null +++ b/Sorts/src/sort/BubbleSort.java @@ -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[] 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)); + + } +}