mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-30 16:06:25 +08:00
Restructured the repo
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.less;
|
||||
import static sort.SortUtils.print;
|
||||
import static Sorts.SortUtils.less;
|
||||
import static Sorts.SortUtils.print;
|
||||
|
||||
/**
|
||||
*
|
@ -1,9 +1,7 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
@ -19,7 +17,7 @@ public class BogoSort implements SortAlgorithm {
|
||||
|
||||
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;
|
||||
if(SortUtils.less(array[i + 1], array[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -30,7 +28,7 @@ public class BogoSort implements SortAlgorithm {
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
int randomIndex = i + random.nextInt(length - i);
|
||||
swap(array, randomIndex, i);
|
||||
SortUtils.swap(array, randomIndex, i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,11 +47,11 @@ public class BogoSort implements SortAlgorithm {
|
||||
BogoSort bogoSort = new BogoSort();
|
||||
|
||||
// print a sorted array
|
||||
print(bogoSort.sort(integers));
|
||||
SortUtils.print(bogoSort.sort(integers));
|
||||
|
||||
// String Input
|
||||
String[] strings = {"c", "a", "e", "b","d"};
|
||||
|
||||
print(bogoSort.sort(strings));
|
||||
SortUtils.print(bogoSort.sort(strings));
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
import static Sorts.SortUtils.*;
|
||||
|
||||
/**
|
||||
*
|
@ -1,6 +1,4 @@
|
||||
package sort;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
package Sorts;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -29,8 +27,8 @@ class CocktailShakerSort implements SortAlgorithm {
|
||||
// front
|
||||
swappedRight = 0;
|
||||
for (int i = left; i < right; i++) {
|
||||
if (less(array[i + 1], array[i])) {
|
||||
swap(array, i, i + 1);
|
||||
if (SortUtils.less(array[i + 1], array[i])) {
|
||||
SortUtils.swap(array, i, i + 1);
|
||||
swappedRight = i;
|
||||
}
|
||||
}
|
||||
@ -38,8 +36,8 @@ class CocktailShakerSort implements SortAlgorithm {
|
||||
right = swappedRight;
|
||||
swappedLeft = length - 1;
|
||||
for (int j = right; j > left; j--) {
|
||||
if (less(array[j], array[j - 1])) {
|
||||
swap(array, j - 1, j);
|
||||
if (SortUtils.less(array[j], array[j - 1])) {
|
||||
SortUtils.swap(array, j - 1, j);
|
||||
swappedLeft = j;
|
||||
}
|
||||
}
|
||||
@ -56,11 +54,11 @@ class CocktailShakerSort implements SortAlgorithm {
|
||||
CocktailShakerSort shakerSort = new CocktailShakerSort();
|
||||
|
||||
// Output => 1 4 6 9 12 23 54 78 231
|
||||
print(shakerSort.sort(integers));
|
||||
SortUtils.print(shakerSort.sort(integers));
|
||||
|
||||
// String Input
|
||||
String[] strings = { "c", "a", "e", "b", "d" };
|
||||
print(shakerSort.sort(strings));
|
||||
SortUtils.print(shakerSort.sort(strings));
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
import static Sorts.SortUtils.*;
|
||||
|
||||
|
||||
/**
|
@ -1,4 +1,4 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.IntStream;
|
||||
@ -6,7 +6,7 @@ import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
import static sort.SortUtils.print;
|
||||
import static Sorts.SortUtils.print;
|
||||
|
||||
/**
|
||||
*
|
@ -1,7 +1,7 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.less;
|
||||
import static sort.SortUtils.print;
|
||||
import static Sorts.SortUtils.less;
|
||||
import static Sorts.SortUtils.print;
|
||||
|
||||
/**
|
||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
@ -1,6 +1,6 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
import static Sorts.SortUtils.*;
|
||||
|
||||
/**
|
||||
* Implementation of gnome sort
|
@ -1,10 +1,10 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
import static Sorts.SortUtils.*;
|
||||
|
||||
/**
|
||||
* Heap Sort Algorithm
|
@ -1,7 +1,7 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.less;
|
||||
import static sort.SortUtils.print;
|
||||
import static Sorts.SortUtils.less;
|
||||
import static Sorts.SortUtils.print;
|
||||
|
||||
/**
|
||||
*
|
@ -1,6 +1,6 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.print;
|
||||
import static Sorts.SortUtils.print;
|
||||
|
||||
/**
|
||||
* This method implements the Generic Merge Sort
|
@ -1,6 +1,6 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
import static Sorts.SortUtils.*;
|
||||
|
||||
/**
|
||||
* Implementation of gnome sort
|
@ -1,6 +1,6 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
import static Sorts.SortUtils.*;
|
||||
|
||||
/**
|
||||
*
|
@ -1,4 +1,4 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -1,6 +1,4 @@
|
||||
package sort;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
package Sorts;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -27,14 +25,14 @@ public class SelectionSort implements SortAlgorithm {
|
||||
int min = i;
|
||||
|
||||
for (int j = i +1 ; j < n; j++) {
|
||||
if (less(arr[j], arr[min])) {
|
||||
if (SortUtils.less(arr[j], arr[min])) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Swapping if index of min is changed
|
||||
if (min != i) {
|
||||
swap(arr, i , min);
|
||||
SortUtils.swap(arr, i , min);
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,13 +49,13 @@ public class SelectionSort implements SortAlgorithm {
|
||||
Integer[] sorted = selectionSort.sort(arr);
|
||||
|
||||
// Output => 1 4 6 9 12 23 54 78 231
|
||||
print(sorted);
|
||||
SortUtils.print(sorted);
|
||||
|
||||
// String Input
|
||||
String[] strings = {"c", "a", "e", "b","d"};
|
||||
String[] sortedStrings = selectionSort.sort(strings);
|
||||
|
||||
//Output => a b c d e
|
||||
print(sortedStrings);
|
||||
SortUtils.print(sortedStrings);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import static sort.SortUtils.*;
|
||||
import static Sorts.SortUtils.*;
|
||||
|
||||
|
||||
/**
|
@ -1,4 +1,4 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package sort;
|
||||
package Sorts;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
Reference in New Issue
Block a user