Skip to content

Commit

Permalink
Created general interface for most algorithms
Browse files Browse the repository at this point in the history
Created utils methods
Refactored ShellSort
  • Loading branch information
nikitap492 committed Apr 9, 2018
1 parent 205aec0 commit 3c40937
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 39 deletions.
60 changes: 21 additions & 39 deletions Sorts/ShellSort.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,50 @@
package Sorts;

import static Sorts.SortUtils.*;


/**
* @author dpunosevac
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SortAlgorithm
*
*/
public class ShellSort {
public class ShellSort implements SortAlgorithm {

/**
* This method implements Generic Shell Sort.
* @param array The array to be sorted
*/
public static void shellSort(Comparable[] array) {
@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;
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) {
exch(array, j, j - h);
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;
h /= 3;
}
}

/**
* Helper method for exchanging places in array
* @param array The array which elements we want to swap
* @param i index of the first element
* @param j index of the second element
*/
private static void exch(Comparable[] array, int i, int j) {
Comparable swap = array[i];
array[i] = array[j];
array[j] = 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
*/
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
return array;
}

public static void main(String[] args) {
// Integer Input
int[] arr1 = {4,23,6,78,1,54,231,9,12};
Integer[] array = new Integer[arr1.length];
Integer[] toSort = {4, 23, 6, 78, 1, 54, 231, 9, 12};

for (int i=0;i<arr1.length;i++) {
array[i] = arr1[i];
}
ShellSort sort = new ShellSort();
Integer[] sorted = sort.sort(toSort);

shellSort(array);
print(sorted);

for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
13 changes: 13 additions & 0 deletions Sorts/SortAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package Sorts;

/**
* The common interface of most algorithms
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
**/
public interface SortAlgorithm {

<T extends Comparable<T>> T[] sort(T[] unsorted);

}
60 changes: 60 additions & 0 deletions Sorts/SortUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package Sorts;

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));
}
}

0 comments on commit 3c40937

Please sign in to comment.