Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Sorts/SlowSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package Sorts;

/**
* @author Amir Hassan (https://github.com/ahsNT)
* @see SortAlgorithm
*/
public class SlowSort implements SortAlgorithm {

@Override
public <T extends Comparable<T>> T[] sort(T[] unsortedArray) {
sort(unsortedArray, 0, unsortedArray.length - 1);
return unsortedArray;
}

private <T extends Comparable<T>> void sort(T[] array, int i, int j) {
if (SortUtils.greaterOrEqual(i, j)) {
return;
}
int m = (i + j) / 2;
sort(array, i, m);
sort(array, m + 1, j);
if (SortUtils.less(array[j], array[m])) {
T temp = array[j];
array[j] = array[m];
array[m] = temp;
}
sort(array, i, j - 1);
}

public static void main(String[] args) {
SlowSort slowSort = new SlowSort();

Integer[] integerArray = {8, 84, 53, 953, 64, 2, 202, 98};
// Print integerArray unsorted
SortUtils.print(integerArray);

slowSort.sort(integerArray);
// Print integerArray sorted
SortUtils.print(integerArray);

String[] stringArray = {"g", "d", "a", "b", "f", "c", "e"};
// Print stringArray unsorted
SortUtils.print(stringArray);

slowSort.sort(stringArray);
// Print stringArray sorted
SortUtils.print(stringArray);
}
}
11 changes: 11 additions & 0 deletions Sorts/SortUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ static <T extends Comparable<T>> boolean greater(T v, T w) {
return v.compareTo(w) > 0;
}

/**
* This method checks if first element is greater than or equal the other element
*
* @param v first element
* @param w second element
* @return true if the first element is greater than or equal the second element
*/
static <T extends Comparable<T>> boolean greaterOrEqual(T v, T w) {
return v.compareTo(w) >= 0;
}

/**
* Prints a list
*
Expand Down