The sorting structure was driven to a general java project structure

Fixed the bugs in QuickSort
Refactored QuickSort
This commit is contained in:
nik
2018-04-09 13:25:02 +03:00
parent 35f21f3b1b
commit 2c356b563f
6 changed files with 103 additions and 113 deletions

View File

@ -0,0 +1,94 @@
package sort;
import static sort.SortUtils.*;
/**
*
* @author Varun Upadhyay (https://github.com/varunu28)
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
*/
class QuickSort implements SortAlgorithm {
/**
* This method implements the Generic Quick Sort
*
* @param array The array to be sorted
* Sorts the array in increasing order
**/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
doSort(array, 0, array.length - 1);
return array;
}
/**
* The sorting process
*
* @param left The first index of an array
* @param right The last index of an array
* @param array The array to be sorted
*
**/
private static <T extends Comparable<T>> void doSort(T[] array, int left, int right) {
if (left < right) {
int pivot = partition(array, left, right);
doSort(array, left, pivot - 1);
doSort(array, pivot , right);
}
}
/**
* This method finds the partition index for an array
*
* @param array The array to be sorted
* @param left The first index of an array
* @param right The last index of an array
* Finds the partition index of an array
**/
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
int mid = (left + right) / 2;
T pivot = array[mid];
while(left <= right) {
while(less(array[left], pivot)){
++left;
}
while(less(pivot, array[right])) {
--right;
}
if(left <= right) {
swap(array, left, right);
++left;
--right;
}
}
return left;
}
// Driver Program
public static void main(String[] args) {
// For integer input
Integer[] array = {3, 4, 1, 32, 0, 1, 5, 12 ,2, 5 ,7 ,8 ,9, 2, 44, 111, 5};
QuickSort quickSort = new QuickSort();
// quickSort.sort(array);
//Output => 0 1 1 2 2 3 4 5 5 5 7 8 9 12 32 44 111
print(array);
String[] stringArray = {"c", "a", "e", "b", "d"};
quickSort.sort(stringArray);
//Output => a b c d e
print(stringArray);
}
}

View File

@ -0,0 +1,63 @@
package sort;
import static sort.SortUtils.*;
/**
*
* @author Varun Upadhyay (https://github.com/varunu28)
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SortAlgorithm
*
*/
public class SelectionSort implements SortAlgorithm {
/**
* This method implements the Generic Selection Sort
*
* @param arr The array to be sorted
* Sorts the array in increasing order
**/
@Override
public <T extends Comparable<T>> T[] sort(T[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
// Initial index of min
int min = i;
for (int j = i +1 ; j < n; j++) {
if (less(arr[j], arr[min])) {
min = j;
}
}
// Swapping if index of min is changed
if (min != i) {
swap(arr, i , min);
}
}
return arr;
}
// Driver Program
public static void main(String[] args) {
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
SelectionSort selectionSort = new SelectionSort();
Integer[] sorted = selectionSort.sort(arr);
// Output => 1 4 6 9 12 23 54 78 231
print(sorted);
// String Input
String[] strings = {"c", "a", "e", "b","d"};
String[] sortedStrings = selectionSort.sort(strings);
//Output => a b c d e
print(sortedStrings);
}
}

View File

@ -0,0 +1,50 @@
package sort;
import static sort.SortUtils.*;
/**
* @author dpunosevac
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SortAlgorithm
*
*/
public class ShellSort implements SortAlgorithm {
/**
* This method implements Generic Shell Sort.
* @param array The array to be sorted
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
int N = array.length;
int h = 1;
while (h < N/3) {
h = 3 * h + 1;
}
while (h >= 1) {
for (int i = h; i < N; i++) {
for (int j = i; j >= h && less(array[j], array[j-h]); j -= h) {
swap(array, j, j - h);
}
}
h /= 3;
}
return array;
}
public static void main(String[] args) {
Integer[] toSort = {4, 23, 6, 78, 1, 54, 231, 9, 12};
ShellSort sort = new ShellSort();
Integer[] sorted = sort.sort(toSort);
print(sorted);
}
}

View File

@ -0,0 +1,13 @@
package sort;
/**
* The common interface of most algorithms
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
**/
public interface SortAlgorithm {
<T extends Comparable<T>> T[] sort(T[] unsorted);
}

View File

@ -0,0 +1,60 @@
package sort;
import java.util.Arrays;
import java.util.List;
/**
* The class contains util methods
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
**/
final class SortUtils {
/**
* Helper method for swapping places in array
* @param array The array which elements we want to swap
* @param idx index of the first element
* @param idy index of the second element
*/
static <T> void swap(T[] array, int idx, int idy){
T swap = array[idx];
array[idx] = array[idy];
array[idy] = swap;
}
/**
* This method checks if first element is less then the other element
* @param v first element
* @param w second element
* @return true if the first element is less then the second element
*/
static <T extends Comparable<T>> boolean less(T v, T w) {
return v.compareTo(w) < 0;
}
/**
* Just print list
* @param toPrint - a list which should be printed
*/
static void print(List<?> toPrint){
toPrint.stream()
.map(Object::toString)
.map(str -> str + " ")
.forEach(System.out::print);
System.out.println();
}
/**
* Prints an array
* @param toPrint - the array which should be printed
*/
static void print(Object[] toPrint){
print(Arrays.asList(toPrint));
}
}