From 3c40937c665ed0ce2f9e1c150251679f0ddd1a32 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 12:05:41 +0300 Subject: [PATCH 01/13] Created general interface for most algorithms Created utils methods Refactored ShellSort --- Sorts/ShellSort.java | 60 ++++++++++++++-------------------------- Sorts/SortAlgorithm.java | 13 +++++++++ Sorts/SortUtils.java | 60 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 39 deletions(-) create mode 100644 Sorts/SortAlgorithm.java create mode 100644 Sorts/SortUtils.java diff --git a/Sorts/ShellSort.java b/Sorts/ShellSort.java index 3a777c2c..31c2c607 100644 --- a/Sorts/ShellSort.java +++ b/Sorts/ShellSort.java @@ -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[] 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> T[] sort(T[] unsorted); + +} diff --git a/Sorts/SortUtils.java b/Sorts/SortUtils.java new file mode 100644 index 00000000..73daf6a1 --- /dev/null +++ b/Sorts/SortUtils.java @@ -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 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 > 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)); + } +} From 35f21f3b1bfb7692a87763e0044f4aff8bc5114d Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 12:12:30 +0300 Subject: [PATCH 02/13] Refactored SelectionSort --- Sorts/SelectionSort.java | 55 +++++++++++++++------------------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/Sorts/SelectionSort.java b/Sorts/SelectionSort.java index 21fa7151..bc0201ce 100644 --- a/Sorts/SelectionSort.java +++ b/Sorts/SelectionSort.java @@ -1,73 +1,60 @@ +package Sorts; + +import static Sorts.SortUtils.*; + /** * * @author Varun Upadhyay (https://github.com/varunu28) * */ -public class SelectionSort { +public class SelectionSort implements SortAlgorithm { /** * This method implements the Generic Selection Sort * * @param arr The array to be sorted - * @param n The count of total number of elements in array * Sorts the array in increasing order **/ - - public static > void SS(T[] arr, int n) { - - for (int i=0;i> 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 1 4 6 9 12 23 54 78 231 - for(int i=0; i a b c d e - for(int i=0; i Date: Mon, 9 Apr 2018 13:25:02 +0300 Subject: [PATCH 03/13] The sorting structure was driven to a general java project structure Fixed the bugs in QuickSort Refactored QuickSort --- Sorts/QuickSort.java | 107 ------------------------ Sorts/src/sort/QuickSort.java | 94 +++++++++++++++++++++ Sorts/{ => src/sort}/SelectionSort.java | 7 +- Sorts/{ => src/sort}/ShellSort.java | 4 +- Sorts/{ => src/sort}/SortAlgorithm.java | 2 +- Sorts/{ => src/sort}/SortUtils.java | 2 +- 6 files changed, 103 insertions(+), 113 deletions(-) delete mode 100644 Sorts/QuickSort.java create mode 100644 Sorts/src/sort/QuickSort.java rename Sorts/{ => src/sort}/SelectionSort.java (90%) rename Sorts/{ => src/sort}/ShellSort.java (94%) rename Sorts/{ => src/sort}/SortAlgorithm.java (93%) rename Sorts/{ => src/sort}/SortUtils.java (98%) diff --git a/Sorts/QuickSort.java b/Sorts/QuickSort.java deleted file mode 100644 index b40dfb34..00000000 --- a/Sorts/QuickSort.java +++ /dev/null @@ -1,107 +0,0 @@ -/** - * - * @author Varun Upadhyay (https://github.com/varunu28) - * - */ - -class QuickSort { - - /** - * This method implements the Generic Quick Sort - * - * @param array The array to be sorted - * @param start The first index of an array - * @param end The last index of an array - * Sorts the array in increasing order - **/ - - public static > void QS(List list, int left, int right) { - if (left>=right) { return } - else - { - int pivot = partition(array, left,right); - QS(list, left, pivot- 1); - QS(list, pivot + 1, right); - } - } - - /** - * This method finds the partition index for an array - * - * @param array The array to be sorted - * @param start The first index of an array - * @param end The last index of an array - * Finds the partition index of an array - **/ - - public static > int partition(List list, int left, int right) { - int mid=(left+right)/2; - T pivot=list.get(mid); - swap(list,mid,right); - while(left=0) - { - ++left; - } - if(left=0) - { - --right; - } - if(left> void swap(List list, int initial, int fin) { - E temp= list.get(initial); - list.set(initial,list.get(fin)); - list.set(fin,temp); - } - - // Driver Program - public static void main(String[] args) { - - // For integer input - ArrayList array = new ArrayList(9); - array = {3,4,1,32,0,2,44,111,5}; - - QS(array, 0, array.size()-1); - - //Output => 0 1 2 3 4 5 32 44 111 - for (int i=0;i array1=new ArrayList(5); - array1 = {"c", "a", "e", "b","d"}; - - QS(array1, 0,array1.size()-1); - - //Output => a b c d e - for(int i=0; i> 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 > 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 > 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); + } +} + diff --git a/Sorts/SelectionSort.java b/Sorts/src/sort/SelectionSort.java similarity index 90% rename from Sorts/SelectionSort.java rename to Sorts/src/sort/SelectionSort.java index bc0201ce..960215a6 100644 --- a/Sorts/SelectionSort.java +++ b/Sorts/src/sort/SelectionSort.java @@ -1,10 +1,13 @@ -package Sorts; +package sort; -import static Sorts.SortUtils.*; +import static sort.SortUtils.*; /** * * @author Varun Upadhyay (https://github.com/varunu28) + * @author Podshivalov Nikita (https://github.com/nikitap492) + * + * @see SortAlgorithm * */ diff --git a/Sorts/ShellSort.java b/Sorts/src/sort/ShellSort.java similarity index 94% rename from Sorts/ShellSort.java rename to Sorts/src/sort/ShellSort.java index 31c2c607..bafd19b1 100644 --- a/Sorts/ShellSort.java +++ b/Sorts/src/sort/ShellSort.java @@ -1,6 +1,6 @@ -package Sorts; +package sort; -import static Sorts.SortUtils.*; +import static sort.SortUtils.*; /** diff --git a/Sorts/SortAlgorithm.java b/Sorts/src/sort/SortAlgorithm.java similarity index 93% rename from Sorts/SortAlgorithm.java rename to Sorts/src/sort/SortAlgorithm.java index 5fd41ece..5c88ba41 100644 --- a/Sorts/SortAlgorithm.java +++ b/Sorts/src/sort/SortAlgorithm.java @@ -1,4 +1,4 @@ -package Sorts; +package sort; /** * The common interface of most algorithms diff --git a/Sorts/SortUtils.java b/Sorts/src/sort/SortUtils.java similarity index 98% rename from Sorts/SortUtils.java rename to Sorts/src/sort/SortUtils.java index 73daf6a1..060d61f2 100644 --- a/Sorts/SortUtils.java +++ b/Sorts/src/sort/SortUtils.java @@ -1,4 +1,4 @@ -package Sorts; +package sort; import java.util.Arrays; import java.util.List; From 5560be8262fbe152dd095c0149b153f21b3f28c4 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 13:41:39 +0300 Subject: [PATCH 04/13] MergeSort was implemented SortAlgorithm --- Sorts/{ => src/sort}/MergeSort.java | 73 ++++++++++++++++------------- Sorts/src/sort/QuickSort.java | 3 ++ 2 files changed, 43 insertions(+), 33 deletions(-) rename Sorts/{ => src/sort}/MergeSort.java (53%) diff --git a/Sorts/MergeSort.java b/Sorts/src/sort/MergeSort.java similarity index 53% rename from Sorts/MergeSort.java rename to Sorts/src/sort/MergeSort.java index b53451af..4f294dc7 100644 --- a/Sorts/MergeSort.java +++ b/Sorts/src/sort/MergeSort.java @@ -1,13 +1,37 @@ +package sort; + +import static sort.SortUtils.print; + /** + * This method implements the Generic Merge Sort + * * * @author Varun Upadhyay (https://github.com/varunu28) + * @author Podshivalov Nikita (https://github.com/nikitap492) + * + * + * @see SortAlgorithm * */ -class MergeSort { +class MergeSort implements SortAlgorithm { + /** * This method implements the Generic Merge Sort + * @param unsorted the array which should be sorted + * @param Comparable class + * @return sorted array + */ + @Override + @SuppressWarnings("unchecked") + public > T[] sort(T[] unsorted) { + T[] tmp = (T[]) new Comparable[unsorted.length]; + doSort(unsorted, tmp, 0, unsorted.length - 1); + return unsorted; + } + + /** * * @param arr The array to be sorted * @param temp The copy of the actual array @@ -15,12 +39,11 @@ class MergeSort { * @param right The last index of the array * Recursively sorts the array in increasing order **/ - - public static > void MS(T[] arr, T[] temp, int left, int right) { + private static > void doSort(T[] arr, T[] temp, int left, int right) { if (left < right) { int mid = left + (right - left) / 2; - MS(arr, temp, left, mid); - MS(arr, temp,mid + 1, right); + doSort(arr, temp, left, mid); + doSort(arr, temp,mid + 1, right); merge(arr, temp, left, mid, right); } @@ -37,16 +60,15 @@ class MergeSort { * merges two parts of an array in increasing order **/ - public static > void merge(T[] arr, T[] temp, int left, int mid, int right) { - for (int i=left;i<=right;i++) { - temp[i] = arr[i]; - } + private static > void merge(T[] arr, T[] temp, int left, int mid, int right) { + System.arraycopy(arr, left, temp, left, right - left + 1); + int i= left; int j = mid + 1; int k = left; - while (i<=mid && j<=right) { + while (i <= mid && j <= right) { if (temp[i].compareTo(temp[j]) <= 0) { arr[k] = temp[i]; i++; @@ -69,32 +91,17 @@ class MergeSort { public static void main(String[] args) { // Integer Input - int[] arr = {4,23,6,78,1,54,231,9,12}; - Integer[] array = new Integer[arr.length]; - for (int i=0;i 1 4 6 9 12 23 54 78 231 - for (int i=0;i a b c d e - for(int i=0; i Date: Mon, 9 Apr 2018 14:04:46 +0300 Subject: [PATCH 05/13] Added the default method to sort a list to the sort interface Changes in InsertionSort and CountingSort --- Sorts/InsertionSort.java | 63 -------------------------- Sorts/{ => src/sort}/CountingSort.java | 50 ++++++++++---------- Sorts/src/sort/InsertionSort.java | 61 +++++++++++++++++++++++++ Sorts/src/sort/SortAlgorithm.java | 8 ++++ 4 files changed, 92 insertions(+), 90 deletions(-) delete mode 100644 Sorts/InsertionSort.java rename Sorts/{ => src/sort}/CountingSort.java (70%) create mode 100644 Sorts/src/sort/InsertionSort.java diff --git a/Sorts/InsertionSort.java b/Sorts/InsertionSort.java deleted file mode 100644 index c77b895f..00000000 --- a/Sorts/InsertionSort.java +++ /dev/null @@ -1,63 +0,0 @@ -/** - * - * @author Varun Upadhyay (https://github.com/varunu28) - * - */ - -class InsertionSort { - - /** - * This method implements the Generic Insertion 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 > void IS(T array[], int last) { - T key; - for (int j=1;j=0 && key.compareTo(array[i]) < 0) { - array[i+1] = array[i]; - i--; - } - // Placing the key (Card) at its correct position in the sorted subarray - array[i+1] = key; - } - } - - // 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[arr1.length]; - for (int i=0;i 1 4 6 9 12 23 54 78 231 - for (int i=0;i a b c d e - for(int i=0; i> T[] sort(T[] unsorted) { + return sort(Arrays.asList(unsorted)).toArray(unsorted); + } /** * This method implements the Generic Counting Sort @@ -24,7 +31,8 @@ class CountingSort { * Sorts the list in increasing order * The method uses list elements as keys in the frequency map **/ - public static > List countingSort(List list) { + @Override + public > List sort(List list) { Map frequency = new TreeMap<>(); // The final output array @@ -46,12 +54,12 @@ class CountingSort { /** * Stream Counting Sort - * The same as method {@link CountingSort#countingSort(List)} but this method uses stream API + * The same as method {@link CountingSort#sort(List)} } but this method uses stream API * * @param list The list to be sorted * **/ - public static > List streamCountingSort(List list) { + private static > List streamSort(List list) { return list.stream() .collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new)) .entrySet() @@ -64,15 +72,16 @@ class CountingSort { public static void main(String[] args) { // Integer Input List unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList()); + CountingSort countingSort = new CountingSort(); System.out.println("Before Sorting:"); - printList(unsortedInts); + print(unsortedInts); // Output => 1 1 4 6 9 9 12 23 23 54 78 231 System.out.println("After Sorting:"); - printList(countingSort(unsortedInts)); + print(countingSort.sort(unsortedInts)); System.out.println("After Sorting By Streams:"); - printList(streamCountingSort(unsortedInts)); + print(streamSort(unsortedInts)); System.out.println("\n------------------------------\n"); @@ -80,27 +89,14 @@ class CountingSort { List unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList()); System.out.println("Before Sorting:"); - printList(unsortedStrings); + print(unsortedStrings); //Output => a a b c c d e f g System.out.println("After Sorting:"); - printList(countingSort(unsortedStrings)); + print(countingSort.sort(unsortedStrings)); System.out.println("After Sorting By Streams:"); - printList(streamCountingSort(unsortedStrings)); + print(streamSort(unsortedStrings)); } - - /** - * Just print list - * @param toPrint - a list which should be printed - */ - private static void printList(List toPrint){ - toPrint.stream() - .map(Object::toString) - .map(str -> str + " ") - .forEach(System.out::print); - - System.out.println(); - } } diff --git a/Sorts/src/sort/InsertionSort.java b/Sorts/src/sort/InsertionSort.java new file mode 100644 index 00000000..59361430 --- /dev/null +++ b/Sorts/src/sort/InsertionSort.java @@ -0,0 +1,61 @@ +package sort; + +import static sort.SortUtils.less; +import static sort.SortUtils.print; + +/** + * + * @author Varun Upadhyay (https://github.com/varunu28) + * @author Podshivalov Nikita (https://github.com/nikitap492) + * + */ + +class InsertionSort implements SortAlgorithm { + + /** + * This method implements the Generic Insertion Sort + * Sorts the array in increasing order + * + * @param array The array to be sorted + * + **/ + + @Override + public > T[] sort(T[] array) { + for (int j = 1; j < array.length; j++) { + + // Picking up the key(Card) + T key = array[j]; + int i = j - 1; + + while (i >= 0 && less(key, array[i])) { + array[i + 1] = array[i]; + i--; + } + // Placing the key (Card) at its correct position in the sorted subarray + array[i + 1] = key; + } + return array; + } + + // Driver Program + public static void main(String[] args) { + // Integer Input + Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; + + InsertionSort sort = new InsertionSort(); + + sort.sort(integers); + + // Output => 1 4 6 9 12 23 54 78 231 + print(integers); + + // String Input + String[] strings = {"c", "a", "e", "b","d"}; + + sort.sort(strings); + + //Output => a b c d e + print(strings); + } +} diff --git a/Sorts/src/sort/SortAlgorithm.java b/Sorts/src/sort/SortAlgorithm.java index 5c88ba41..6725d187 100644 --- a/Sorts/src/sort/SortAlgorithm.java +++ b/Sorts/src/sort/SortAlgorithm.java @@ -1,5 +1,8 @@ package sort; +import java.util.Arrays; +import java.util.List; + /** * The common interface of most algorithms * @@ -10,4 +13,9 @@ public interface SortAlgorithm { > T[] sort(T[] unsorted); + @SuppressWarnings("unchecked") + default > List sort(List unsorted){ + return Arrays.asList(sort(unsorted.toArray((T[]) new Comparable[unsorted.size()]))); + } + } From 65361a44455cb1bc7e5e2a4f57bdf13e5c0f51af Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 15:12:40 +0300 Subject: [PATCH 06/13] Refactored and fixed the bugs in BinaryTreeSort --- Sorts/BinaryTreeSort.java | 80 -------------------------- Sorts/src/sort/BinaryTreeSort.java | 90 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 80 deletions(-) delete mode 100644 Sorts/BinaryTreeSort.java create mode 100644 Sorts/src/sort/BinaryTreeSort.java diff --git a/Sorts/BinaryTreeSort.java b/Sorts/BinaryTreeSort.java deleted file mode 100644 index e6a80f62..00000000 --- a/Sorts/BinaryTreeSort.java +++ /dev/null @@ -1,80 +0,0 @@ -import java.util.Arrays; -public class TreeSort { - - public Node root; - - public TreeSort(Object x) { - root = new Node(x); - }//end TreeSort constructor - - public Node insert(Node node, Integer x) { - if (node == null) { - return node = new Node(x); - }//end if - if (x < (Integer) node.anElement) { - node.less = insert(node.less, x); - } //end if - else { - node.greater = insert(node.greater, x); - }//end else - return node; - }//end insert - - - public Node decimalInsert(Node node, Double x) { - if (node == null) { - return node = new Node(x); - }//end if - if (x < (Double) node.anElement) { - node.less = decimalInsert(node.less, x); - } //end if - else { - node.greater = decimalInsert(node.greater, x); - }//end else - return node; - }//end insert - - - public void treeSort(Node node) { - if (node != null) { - treeSort(node.less); - System.out.print(((Object) node.anElement) + ", "); - treeSort(node.greater); - }//end if - }//end TreeSort class - - - -public static void main(String args[]) { - int[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12 }; - TreeSort ts = new TreeSort(new Integer(intArray[0])); - for (int i = 1; i < intArray.length; i++) { //sorts every index of the list one at a time - ts.insert(ts.root, new Integer(intArray[i])); //builds on the tree from a root node - }//end for - System.out.print("Integer Array Sorted in Increasing Order: "); - ts.treeSort(ts.root); - System.out.println(); //To sort a test array of integers - - Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6}; - TreeSort dts = new TreeSort(new Double(decimalArray[0]).doubleValue()); - for (int i = 1; i < decimalArray.length; i++) { //sorts every index of the list one at a time - dts.decimalInsert(dts.root, new Double(decimalArray[i]).doubleValue()); //builds on the tree from a root node - }//end for - System.out.print("Decimal Array, Sorted in Increasing Order: "); - dts.treeSort(dts.root); - System.out.println(); - - String[] stringArray = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; - int last = stringArray.length; - Arrays.sort(stringArray); //Uses an imported arrays method to automatically alphabetize - System.out.print("String Array Sorted in Alphabetical Order: "); - ts.insert(ts.root, last); - for(int i=0; i> { + void visit(Node node); + } + + private static class SortVisitor> implements TreeVisitor { + + private final T[] array; + private int counter; + + SortVisitor(T[] array) { + this.array = array; + } + + @Override + public void visit(Node node) { + array[counter++] = node.value; + } + } + + private static class Node>{ + private T value; + private Node left; + private Node right; + + Node(T value) { + this.value = value; + } + + void insert(Node node) { + if (less(node.value, value)){ + if (left != null) left.insert(node); + else left = node; + } + else { + if (right != null) right.insert(node); + else right = node; + } + } + + void traverse(TreeVisitor visitor) { + if ( left != null) + left.traverse(visitor); + + visitor.visit(this); + + if ( right != null ) + right.traverse(visitor ); + } + + } + + + private > T[] sort(T[] array) { + + Node root = new Node<>(array[0]); + for (int i = 1; i < array.length; i++) { + root.insert(new Node<>(array[i])); + } + + root.traverse(new SortVisitor<>(array)); + + return array; + + } + + + public static void main(String args[]) { + + Integer[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12}; + BinaryTreeSort treeSort = new BinaryTreeSort(); + Integer[] sorted = treeSort.sort(intArray); + print(sorted); + + Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6}; + print(treeSort.sort(decimalArray)); + + String[] stringArray = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; + print(treeSort.sort(stringArray)); + } + +} + From b01c2cf2c6162de3d8f1c50541acf8d6eb3a92cd Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 15:24:16 +0300 Subject: [PATCH 07/13] Refactored bogo sort and bubble sort --- Sorts/BogoSort.java | 68 ----------------------------- Sorts/BubbleSort.java | 70 ------------------------------ Sorts/src/sort/BinaryTreeSort.java | 14 ++++-- Sorts/src/sort/BogoSort.java | 59 +++++++++++++++++++++++++ Sorts/src/sort/BubbleSort.java | 58 +++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 142 deletions(-) delete mode 100644 Sorts/BogoSort.java delete mode 100644 Sorts/BubbleSort.java create mode 100644 Sorts/src/sort/BogoSort.java create mode 100644 Sorts/src/sort/BubbleSort.java diff --git a/Sorts/BogoSort.java b/Sorts/BogoSort.java deleted file mode 100644 index 33787178..00000000 --- a/Sorts/BogoSort.java +++ /dev/null @@ -1,68 +0,0 @@ -package Sorts; - -import java.util.Random; - -public class BogoSort { - private static void swap(T array[], int first, int second){ - T randomElement = array[first]; - array[first] = array[second]; - array[second] = randomElement; - } - - private static > boolean isSorted(T array[]){ - for(int i = 0; i 0) return false; - } - return true; - } - - // Randomly shuffles the array - private static 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 > 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 1 4 6 9 12 23 54 78 231 - for(int i=0; i a b c d e - for(int i=0; i> 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 1 4 6 9 12 23 54 78 231 - for(int i=0; i a b c d e - for(int i=0; i> { void visit(Node node); @@ -58,7 +64,8 @@ public class BinaryTreeSort{ } - private > T[] sort(T[] array) { + @Override + public > T[] sort(T[] array) { Node root = new Node<>(array[0]); for (int i = 1; i < array.length; i++) { @@ -68,7 +75,6 @@ public class BinaryTreeSort{ root.traverse(new SortVisitor<>(array)); return array; - } diff --git a/Sorts/src/sort/BogoSort.java b/Sorts/src/sort/BogoSort.java new file mode 100644 index 00000000..b367df85 --- /dev/null +++ b/Sorts/src/sort/BogoSort.java @@ -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 > boolean isSorted(T array[]){ + for(int i = 0; i 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[] 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)); + } +} diff --git a/Sorts/src/sort/BubbleSort.java b/Sorts/src/sort/BubbleSort.java new file mode 100644 index 00000000..67782cf0 --- /dev/null +++ b/Sorts/src/sort/BubbleSort.java @@ -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[] 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)); + + } +} From 52d1182de5881620824a21367c177f5286d54596 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 15:48:08 +0300 Subject: [PATCH 08/13] Refactored BubbleSort, CycleSort, CocktailShakerSort --- Sorts/CocktailShakerSort.java | 86 -------------------------- Sorts/src/sort/BubbleSort.java | 11 ++-- Sorts/src/sort/CocktailShakerSort.java | 70 +++++++++++++++++++++ Sorts/src/sort/CycleSort.java | 81 ++++++++++++++++++++++++ Sorts/src/sort/SortUtils.java | 3 +- 5 files changed, 157 insertions(+), 94 deletions(-) delete mode 100644 Sorts/CocktailShakerSort.java create mode 100644 Sorts/src/sort/CocktailShakerSort.java create mode 100644 Sorts/src/sort/CycleSort.java diff --git a/Sorts/CocktailShakerSort.java b/Sorts/CocktailShakerSort.java deleted file mode 100644 index 4db840d9..00000000 --- a/Sorts/CocktailShakerSort.java +++ /dev/null @@ -1,86 +0,0 @@ - -/** - * - * @author Mateus Bizzo (https://github.com/MattBizzo) - * - */ - -class CocktailShakerSort { - /** - * This method implements the Generic Cocktail Shaker 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 > void CS(T array[], int last) { - - // Sorting - boolean swap; - do { - swap = false; - - //front - for (int count = 0; count <= last - 2; count++) { - int comp = array[count].compareTo(array[count + 1]); - if (comp > 0) { - T aux = array[count]; - array[count] = array[count + 1]; - array[count + 1] = aux; - swap = true; - } - } - //break if no swap occurred - if (!swap) { - break; - } - swap = false; - - //back - for (int count = last - 2; count >= 0; count--) { - int comp = array[count].compareTo(array[count + 1]); - if (comp > 0) { - T aux = array[count]; - array[count] = array[count + 1]; - array[count + 1] = aux; - swap = true; - } - } - last--; - //end - } 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]; - } - - CS(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; - - CS(array1, last); - - // Output => a b c d e - for (int i = 0; i < last; i++) { - System.out.print(array1[i] + "\t"); - } - } -} diff --git a/Sorts/src/sort/BubbleSort.java b/Sorts/src/sort/BubbleSort.java index 67782cf0..0b8e8da9 100644 --- a/Sorts/src/sort/BubbleSort.java +++ b/Sorts/src/sort/BubbleSort.java @@ -1,7 +1,6 @@ package sort; -import static sort.SortUtils.print; -import static sort.SortUtils.swap; +import static sort.SortUtils.*; /** * @@ -27,10 +26,8 @@ class BubbleSort implements SortAlgorithm{ 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; + if (less(array[count], array[count + 1])) { + swap = swap(array, count, count + 1); } } last--; @@ -42,7 +39,7 @@ class BubbleSort implements SortAlgorithm{ public static void main(String[] args) { // Integer Input - Integer[] integers = {4,23,6,78,1,54,231,9,12}; + Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; BubbleSort bubbleSort = new BubbleSort(); bubbleSort.sort(integers); diff --git a/Sorts/src/sort/CocktailShakerSort.java b/Sorts/src/sort/CocktailShakerSort.java new file mode 100644 index 00000000..b5da0df5 --- /dev/null +++ b/Sorts/src/sort/CocktailShakerSort.java @@ -0,0 +1,70 @@ +package sort; + +import static sort.SortUtils.*; + +/** + * + * @author Mateus Bizzo (https://github.com/MattBizzo) + * @author Podshivalov Nikita (https://github.com/nikitap492) + * + */ + +class CocktailShakerSort implements SortAlgorithm { + + /** + * This method implements the Generic Cocktail Shaker Sort + * + * @param array The array to be sorted + * Sorts the array in increasing order + **/ + + @Override + public > T[] sort(T[] array){ + + int last = array.length; + + // Sorting + boolean swap; + do { + swap = false; + + //front + for (int count = 0; count <= last - 2; count++) { + if (less(array[count + 1], array[count])) { + swap = swap(array, count, count + 1); + } + } + //break if no swap occurred + if (!swap) { + break; + } + swap = false; + + //back + for (int count = last - 2; count >= 0; count--) { + if (less(array[count + 1], array[count])) { + swap = swap(array, count, count + 1); + } + } + last--; + //end + } 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 }; + CocktailShakerSort shakerSort = new CocktailShakerSort(); + + // Output => 1 4 6 9 12 23 54 78 231 + print(shakerSort.sort(integers)); + + // String Input + String[] strings = { "c", "a", "e", "b", "d" }; + print(shakerSort.sort(strings)); + } + + +} diff --git a/Sorts/src/sort/CycleSort.java b/Sorts/src/sort/CycleSort.java new file mode 100644 index 00000000..ea4f0553 --- /dev/null +++ b/Sorts/src/sort/CycleSort.java @@ -0,0 +1,81 @@ +package sort; + +import static sort.SortUtils.less; +import static sort.SortUtils.print; + +/** + * @author Podshivalov Nikita (https://github.com/nikitap492) + */ +class CycleSort implements SortAlgorithm { + + + @Override + public > T[] sort(T[] arr) { + int n = arr.length; + + // traverse array elements + for (int j = 0; j <= n - 2; j++) { + // initialize item as starting point + T item = arr[j]; + + // Find position where we put the item. + int pos = j; + for (int i = j + 1; i < n; i++) + if (less(arr[i], item)) pos++; + + // If item is already in correct position + if (pos == j) continue; + + // ignore all duplicate elements + while (item.compareTo(arr[pos]) == 0) + pos += 1; + + // put the item to it's right position + if (pos != j) { + item = replace(arr, pos, item); + } + + // Rotate rest of the cycle + while (pos != j) { + pos = j; + + // Find position where we put the element + for (int i = j + 1; i < n; i++) + if (less(arr[i], item)){ + pos += 1; + } + + + // ignore all duplicate elements + while (item.compareTo(arr[pos]) == 0) + pos += 1; + + // put the item to it's right position + if (item != arr[pos]) { + item = replace(arr, pos, item); + } + } + } + + return arr; + } + + private > T replace(T[] arr, int pos, T item){ + T temp = item; + item = arr[pos]; + arr[pos] = temp; + return item; + } + + + + public static void main(String[] args) { + Integer arr[] = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 }; + CycleSort cycleSort = new CycleSort(); + cycleSort.sort(arr); + + System.out.println("After sort : "); + print(arr); + } + +} diff --git a/Sorts/src/sort/SortUtils.java b/Sorts/src/sort/SortUtils.java index 060d61f2..e1cf8482 100644 --- a/Sorts/src/sort/SortUtils.java +++ b/Sorts/src/sort/SortUtils.java @@ -18,10 +18,11 @@ final class SortUtils { * @param idx index of the first element * @param idy index of the second element */ - static void swap(T[] array, int idx, int idy){ + static boolean swap(T[] array, int idx, int idy){ T swap = array[idx]; array[idx] = array[idy]; array[idy] = swap; + return true; } From d4bd9e7c7de90310c1c77e78718c11d4d049d648 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 15:50:48 +0300 Subject: [PATCH 09/13] Replaced the left sorts to the new package --- Sorts/cyclesort.java | 78 ------------------- Sorts/{ => src/sort}/HeapSort.java | 0 .../sort/RadixSort.java} | 16 ++-- 3 files changed, 8 insertions(+), 86 deletions(-) delete mode 100644 Sorts/cyclesort.java rename Sorts/{ => src/sort}/HeapSort.java (100%) rename Sorts/{radixSort.java => src/sort/RadixSort.java} (83%) diff --git a/Sorts/cyclesort.java b/Sorts/cyclesort.java deleted file mode 100644 index 5b7e5652..00000000 --- a/Sorts/cyclesort.java +++ /dev/null @@ -1,78 +0,0 @@ -import java.util.*; -import java.lang.*; - -class Sorting -{ - // Function that sort the array using Cycle sort - public static void cycleSort (int arr[], int n) - { - // count number of memory writes - int writes = 0; - - // traverse array elements - for (int cycle_start=0; cycle_start<=n-2; cycle_start++) - { - // initialize item as starting point - int item = arr[cycle_start]; - - // Find position where we put the item. - int pos = cycle_start; - for (int i = cycle_start+1; i mx) mx = arr[i]; return mx; } - - static void countSort(int arr[], int n, int exp) + + private static void countSort(int arr[], int n, int exp) { int output[] = new int[n]; int i; @@ -35,8 +36,7 @@ class Radix { arr[i] = output[i]; } - static void radixsort(int arr[], int n) - { + private static void radixsort(int arr[], int n) { int m = getMax(arr, n); From 9491b45a05a3c9532d7f3cd1d19d664532e9e3c1 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 21:14:40 +0300 Subject: [PATCH 10/13] Minor changes --- Sorts/src/sort/BinaryTreeSort.java | 2 +- Sorts/src/sort/BogoSort.java | 2 +- Sorts/src/sort/BubbleSort.java | 2 +- Sorts/src/sort/HeapSort.java | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Sorts/src/sort/BinaryTreeSort.java b/Sorts/src/sort/BinaryTreeSort.java index 518016c0..59d58ab6 100644 --- a/Sorts/src/sort/BinaryTreeSort.java +++ b/Sorts/src/sort/BinaryTreeSort.java @@ -10,7 +10,7 @@ import static sort.SortUtils.print; * @see SortAlgorithm * */ -public class BinaryTreeSort implements SortAlgorithm{ +public class BinaryTreeSort implements SortAlgorithm { interface TreeVisitor> { void visit(Node node); diff --git a/Sorts/src/sort/BogoSort.java b/Sorts/src/sort/BogoSort.java index b367df85..1079ca21 100644 --- a/Sorts/src/sort/BogoSort.java +++ b/Sorts/src/sort/BogoSort.java @@ -12,7 +12,7 @@ import static sort.SortUtils.*; * @see SortAlgorithm * */ -public class BogoSort implements SortAlgorithm{ +public class BogoSort implements SortAlgorithm { private static final Random random = new Random(); diff --git a/Sorts/src/sort/BubbleSort.java b/Sorts/src/sort/BubbleSort.java index 0b8e8da9..99f63907 100644 --- a/Sorts/src/sort/BubbleSort.java +++ b/Sorts/src/sort/BubbleSort.java @@ -10,7 +10,7 @@ import static sort.SortUtils.*; * @see SortAlgorithm */ -class BubbleSort implements SortAlgorithm{ +class BubbleSort implements SortAlgorithm { /** * This method implements the Generic Bubble Sort * diff --git a/Sorts/src/sort/HeapSort.java b/Sorts/src/sort/HeapSort.java index 6b3a3fdc..c5bcca10 100644 --- a/Sorts/src/sort/HeapSort.java +++ b/Sorts/src/sort/HeapSort.java @@ -1,3 +1,5 @@ +package sort; + import java.util.Scanner; /** From 18a514857684b95c02c5cd01a65a14b344c9765c Mon Sep 17 00:00:00 2001 From: nik Date: Tue, 10 Apr 2018 11:16:28 +0300 Subject: [PATCH 11/13] Refactored HeapSort --- Sorts/src/sort/HeapSort.java | 243 ++++++++++++++--------------------- 1 file changed, 99 insertions(+), 144 deletions(-) diff --git a/Sorts/src/sort/HeapSort.java b/Sorts/src/sort/HeapSort.java index c5bcca10..1d009dcf 100644 --- a/Sorts/src/sort/HeapSort.java +++ b/Sorts/src/sort/HeapSort.java @@ -1,6 +1,10 @@ package sort; -import java.util.Scanner; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static sort.SortUtils.*; /** * Heap Sort Algorithm @@ -9,168 +13,119 @@ import java.util.Scanner; * @author Unknown * */ -public class HeapSort { - /** Array to store heap */ - private int[] heap; - /** The size of the heap */ - private int size; +public class HeapSort implements SortAlgorithm { - /** - * Constructor - * - * @param heap array of unordered integers - */ - public HeapSort(int[] heap) { - this.setHeap(heap); - this.setSize(heap.length); - } - /** - * Setter for variable size - * - * @param length new size - */ - private void setSize(int length) { - this.size = length; - } + private static class Heap> { + /** Array to store heap */ + private T[] heap; - /** - * Setter for variable heap - * - * @param heap array of unordered elements - */ - private void setHeap(int[] heap) { - this.heap = heap; - } + /** + * Constructor + * + * @param heap array of unordered integers + */ + public Heap(T[] heap) { + this.heap = heap; + } - /** - * Swaps index of first with second - * - * @param first First index to switch - * @param second Second index to switch - */ - private void swap(int first, int second) { - int temp = this.heap[first]; - this.heap[first] = this.heap[second]; - this.heap[second] = temp; - } - - /** - * Heapifies subtree from top as root to last as last child - * - * @param rootIndex index of root - * @param lastChild index of last child - */ - private void heapSubtree(int rootIndex, int lastChild) { - int leftIndex = rootIndex * 2 + 1; - int rightIndex = rootIndex * 2 + 2; - int root = this.heap[rootIndex]; - if (rightIndex <= lastChild) { // if has right and left children - int left = this.heap[leftIndex]; - int right = this.heap[rightIndex]; - if (left < right && left < root) { - this.swap(leftIndex, rootIndex); - this.heapSubtree(leftIndex, lastChild); - } else if (right < root) { - this.swap(rightIndex, rootIndex); - this.heapSubtree(rightIndex, lastChild); - } - } else if (leftIndex <= lastChild) { // if no right child, but has left child - int left = this.heap[leftIndex]; - if (left < root) { - this.swap(leftIndex, rootIndex); - this.heapSubtree(leftIndex, lastChild); + /** + * Heapifies subtree from top as root to last as last child + * + * @param rootIndex index of root + * @param lastChild index of last child + */ + private void heapSubtree(int rootIndex, int lastChild) { + int leftIndex = rootIndex * 2 + 1; + int rightIndex = rootIndex * 2 + 2; + T root = heap[rootIndex]; + if (rightIndex <= lastChild) { // if has right and left children + T left = heap[leftIndex]; + T right = heap[rightIndex]; + if (less(left, right) && less(left, root)) { + swap(heap, leftIndex, rootIndex); + heapSubtree(leftIndex, lastChild); + } else if (less(right, root)) { + swap(heap, rightIndex, rootIndex); + heapSubtree(rightIndex, lastChild); + } + } else if (leftIndex <= lastChild) { // if no right child, but has left child + T left = heap[leftIndex]; + if (less(left, root)) { + swap(heap, leftIndex, rootIndex); + heapSubtree(leftIndex, lastChild); + } } } - } - /** - * Makes heap with root as root - * - * @param root index of root of heap - */ - private void makeMinHeap(int root) { - int leftIndex = root * 2 + 1; - int rightIndex = root * 2 + 2; - boolean hasLeftChild = leftIndex < this.heap.length; - boolean hasRightChild = rightIndex < this.heap.length; - if (hasRightChild) { //if has left and right - this.makeMinHeap(leftIndex); - this.makeMinHeap(rightIndex); - this.heapSubtree(root, this.heap.length - 1); - } else if (hasLeftChild) { - this.heapSubtree(root, this.heap.length - 1); + + /** + * Makes heap with root as root + * + * @param root index of root of heap + */ + private void makeMinHeap(int root) { + int leftIndex = root * 2 + 1; + int rightIndex = root * 2 + 2; + boolean hasLeftChild = leftIndex < heap.length; + boolean hasRightChild = rightIndex < heap.length; + if (hasRightChild) { //if has left and right + makeMinHeap(leftIndex); + makeMinHeap(rightIndex); + heapSubtree(root, heap.length - 1); + } else if (hasLeftChild) { + heapSubtree(root, heap.length - 1); + } } - } - /** - * Gets the root of heap - * - * @return root of heap - */ - private int getRoot() { - this.swap(0, this.size - 1); - this.size--; - this.heapSubtree(0, this.size - 1); - return this.heap[this.size]; // return old root - } - - /** - * Sorts heap with heap sort; displays ordered elements to console. - * - * @return sorted array of sorted elements - */ - public final int[] sort() { - this.makeMinHeap(0); // make min heap using index 0 as root. - int[] sorted = new int[this.size]; - int index = 0; - while (this.size > 0) { - int min = this.getRoot(); - sorted[index] = min; - index++; + /** + * Gets the root of heap + * + * @return root of heap + */ + private T getRoot(int size) { + swap(heap, 0, size); + heapSubtree(0, size - 1); + return heap[size]; // return old root } + + + + + + } + + @Override + public > T[] sort(T[] unsorted) { + return sort(Arrays.asList(unsorted)).toArray(unsorted); + } + + @Override + public > List sort(List unsorted) { + int size = unsorted.size(); + + @SuppressWarnings("unchecked") + Heap heap = new Heap<>(unsorted.toArray((T[]) new Comparable[unsorted.size()])); + + heap.makeMinHeap(0); // make min heap using index 0 as root. + List sorted = new ArrayList<>(size); + while (size > 0) { + T min = heap.getRoot(--size); + sorted.add(min); + } + return sorted; } - /** - * Gets input to sort - * - * @return unsorted array of integers to sort - */ - public static int[] getInput() { - final int numElements = 6; - int[] unsorted = new int[numElements]; - Scanner input = new Scanner(System.in); - System.out.println("Enter any 6 Numbers for Unsorted Array : "); - for (int i = 0; i < numElements; i++) { - unsorted[i] = input.nextInt(); - } - input.close(); - return unsorted; - } - - /** - * Prints elements in heap - * - * @param heap array representing heap - */ - public static void printData(int[] heap) { - System.out.println("Sorted Elements:"); - for (int i = 0; i < heap.length; i++) { - System.out.print(" " + heap[i] + " "); - } - } - /** * Main method * * @param args the command line arguments */ public static void main(String[] args) { - int[] heap = getInput(); - HeapSort data = new HeapSort(heap); - int[] sorted = data.sort(); - printData(sorted); + Integer[] heap = {4, 23, 6, 78, 1, 54, 231, 9, 12}; + HeapSort heapSort = new HeapSort(); + print(heapSort.sort(heap)); } } From e15cfb29b12cc6d351bcb82c43ca4deb9ff71d79 Mon Sep 17 00:00:00 2001 From: nik Date: Tue, 10 Apr 2018 11:32:49 +0300 Subject: [PATCH 12/13] Added GnomeSort --- Sorts/src/sort/GnomeSort.java | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Sorts/src/sort/GnomeSort.java diff --git a/Sorts/src/sort/GnomeSort.java b/Sorts/src/sort/GnomeSort.java new file mode 100644 index 00000000..14af67c6 --- /dev/null +++ b/Sorts/src/sort/GnomeSort.java @@ -0,0 +1,44 @@ +package sort; + +import static sort.SortUtils.*; + +/** + * Implementation of gnome sort + * + * @author Podshivalov Nikita (https://github.com/nikitap492) + * @since 2018-04-10 + * + **/ +public class GnomeSort implements SortAlgorithm{ + + @Override + public > T[] sort(T[] arr) { + int i = 1; + int j = 2; + while (i < arr.length){ + if ( less(arr[i - 1], arr[i]) ) i = j++; + else { + swap(arr, i - 1, i); + if (--i == 0){ i = j++; } + } + } + + return null; + } + + public static void main(String[] args) { + Integer[] integers = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 }; + String[] strings = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; + GnomeSort gnomeSort = new GnomeSort(); + + gnomeSort.sort(integers); + gnomeSort.sort(strings); + + System.out.println("After sort : "); + print(integers); + print(strings); + + + } + +} From e83317fd89f25fcd0927af3624a4c8f53bcf6c46 Mon Sep 17 00:00:00 2001 From: nik Date: Tue, 10 Apr 2018 12:48:21 +0300 Subject: [PATCH 13/13] Added PancakeSort and the other minor changes --- Sorts/src/sort/HeapSort.java | 4 +-- Sorts/src/sort/PancakeSort.java | 47 +++++++++++++++++++++++++++++++++ Sorts/src/sort/SortUtils.java | 16 ++++++++++- 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 Sorts/src/sort/PancakeSort.java diff --git a/Sorts/src/sort/HeapSort.java b/Sorts/src/sort/HeapSort.java index 1d009dcf..6fab3747 100644 --- a/Sorts/src/sort/HeapSort.java +++ b/Sorts/src/sort/HeapSort.java @@ -9,8 +9,8 @@ import static sort.SortUtils.*; /** * Heap Sort Algorithm * Implements MinHeap - * - * @author Unknown + * + * @author Podshivalov Nikita (https://github.com/nikitap492) * */ public class HeapSort implements SortAlgorithm { diff --git a/Sorts/src/sort/PancakeSort.java b/Sorts/src/sort/PancakeSort.java new file mode 100644 index 00000000..902db4b3 --- /dev/null +++ b/Sorts/src/sort/PancakeSort.java @@ -0,0 +1,47 @@ +package sort; + +import static sort.SortUtils.*; + +/** + * Implementation of gnome sort + * + * @author Podshivalov Nikita (https://github.com/nikitap492) + * @since 2018-04-10 + * + **/ +public class PancakeSort implements SortAlgorithm { + + + @Override + public > T[] sort(T[] array){ + int size = array.length; + + for (int i = 0; i < size; i++) { + T max = array[0]; + int index = 0; + for (int j = 0; j < size - i; j++) { + if ( less(max, array[j]) ) { + max = array[j]; + index = j; + } + } + flip(array, index, array.length - 1 - i); + } + return array; + } + + + public static void main(String[] args) { + + Integer[] arr = {10, 9, 8, 7, 6, 15, 14, 7, 4, 3, 8, 6, 3, 1 ,2, -2, -5, -8, -3, -1, 13, 12, 11, 5, 4, 3, 2, 1}; + PancakeSort pancakeSort = new PancakeSort(); + System.out.println("After sorting:"); + pancakeSort.sort(arr); + print(arr); + } + + + + + +} diff --git a/Sorts/src/sort/SortUtils.java b/Sorts/src/sort/SortUtils.java index e1cf8482..8766e9d0 100644 --- a/Sorts/src/sort/SortUtils.java +++ b/Sorts/src/sort/SortUtils.java @@ -56,6 +56,20 @@ final class SortUtils { * @param toPrint - the array which should be printed */ static void print(Object[] toPrint){ - print(Arrays.asList(toPrint)); + System.out.println(Arrays.toString(toPrint)); + } + + + + /** + * Swaps all position from {@param left} to @{@param right} for {@param array} + * @param array is an array + * @param left is a left flip border of the array + * @param right is a right flip border of the array + */ + static > void flip(T[] array, int left, int right) { + while (left <= right) { + swap(array, left++ , right--); + } } }