mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 05:59:22 +08:00
Refactored bogo sort and bubble sort
This commit is contained in:
@ -1,68 +0,0 @@
|
|||||||
package Sorts;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class BogoSort {
|
|
||||||
private static <T> void swap(T array[], int first, int second){
|
|
||||||
T randomElement = array[first];
|
|
||||||
array[first] = array[second];
|
|
||||||
array[second] = randomElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <T extends Comparable<T>> boolean isSorted(T array[]){
|
|
||||||
for(int i = 0; i<array.length-1; i++){
|
|
||||||
if(array[i].compareTo(array[i+1]) > 0) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Randomly shuffles the array
|
|
||||||
private static <T> 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 <T extends Comparable<T>> 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<last;i++) {
|
|
||||||
array[i] = arr1[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
bogoSort(array);
|
|
||||||
|
|
||||||
// Output => 1 4 6 9 12 23 54 78 231
|
|
||||||
for(int i=0; i<last; i++)
|
|
||||||
{
|
|
||||||
System.out.print(array[i]+"\t");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
// String Input
|
|
||||||
String[] array1 = {"c", "a", "e", "b","d"};
|
|
||||||
last = array1.length;
|
|
||||||
|
|
||||||
bogoSort(array1);
|
|
||||||
|
|
||||||
//Output => a b c d e
|
|
||||||
for(int i=0; i<last; i++)
|
|
||||||
{
|
|
||||||
System.out.print(array1[i]+"\t");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,70 +0,0 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
class BubbleSort
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* This method implements the Generic Bubble Sort
|
|
||||||
*
|
|
||||||
* @param array The array to be sorted
|
|
||||||
* @param last The count of total number of elements in array
|
|
||||||
* Sorts the array in increasing order
|
|
||||||
**/
|
|
||||||
|
|
||||||
public static <T extends Comparable<T>> 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<last;i++) {
|
|
||||||
array[i] = arr1[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
BS(array, last);
|
|
||||||
|
|
||||||
// Output => 1 4 6 9 12 23 54 78 231
|
|
||||||
for(int i=0; i<last; i++)
|
|
||||||
{
|
|
||||||
System.out.print(array[i]+"\t");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
// String Input
|
|
||||||
String[] array1 = {"c", "a", "e", "b","d"};
|
|
||||||
last = array1.length;
|
|
||||||
|
|
||||||
BS(array1, last);
|
|
||||||
|
|
||||||
//Output => a b c d e
|
|
||||||
for(int i=0; i<last; i++)
|
|
||||||
{
|
|
||||||
System.out.print(array1[i]+"\t");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,8 +3,14 @@ package sort;
|
|||||||
import static sort.SortUtils.less;
|
import static sort.SortUtils.less;
|
||||||
import static sort.SortUtils.print;
|
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>> {
|
interface TreeVisitor<T extends Comparable<T>> {
|
||||||
void visit(Node<T> node);
|
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]);
|
Node<T> root = new Node<>(array[0]);
|
||||||
for (int i = 1; i < array.length; i++) {
|
for (int i = 1; i < array.length; i++) {
|
||||||
@ -68,7 +75,6 @@ public class BinaryTreeSort{
|
|||||||
root.traverse(new SortVisitor<>(array));
|
root.traverse(new SortVisitor<>(array));
|
||||||
|
|
||||||
return array;
|
return array;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
59
Sorts/src/sort/BogoSort.java
Normal file
59
Sorts/src/sort/BogoSort.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
58
Sorts/src/sort/BubbleSort.java
Normal file
58
Sorts/src/sort/BubbleSort.java
Normal 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));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user