From cf778675df4aaa2e5a964a2922a8431ccc913edf Mon Sep 17 00:00:00 2001 From: nik Date: Thu, 12 Apr 2018 10:14:22 +0300 Subject: [PATCH] Comb sort was implemented --- Sorts/CombSort.java | 62 ------------------------- Sorts/src/sort/CombSort.java | 76 +++++++++++++++++++++++++++++++ Sorts/src/sort/SortAlgorithm.java | 10 ++++ 3 files changed, 86 insertions(+), 62 deletions(-) delete mode 100644 Sorts/CombSort.java create mode 100644 Sorts/src/sort/CombSort.java diff --git a/Sorts/CombSort.java b/Sorts/CombSort.java deleted file mode 100644 index 16266e362ed8..000000000000 --- a/Sorts/CombSort.java +++ /dev/null @@ -1,62 +0,0 @@ -// Java program for implementation of Comb Sort - -class CombSort -{ - // To find gap between elements - static int getNextGap(int gap) - { - // Shrink gap by Shrink factor - gap = (gap*10)/13; - gap = (gap < 1) ? 1: gap; - } - - // Function to sort arr[] using Comb Sort - static void sort(int arr[]) - { - int n = arr.length; - - // initialize gap - int gap = n; - - // Initialize swapped as true to make sure that loop runs - boolean swapped = true; - - // Keep running while gap is more than 1 and last iteration caused a swap - while (gap != 1 || swapped) - { - // Find next gap - gap = getNextGap(gap); - - // Initialize swapped as false so that we can check if swap happened or not - swapped = false; - - // Compare all elements with current gap - for (int i=0; i arr[i+gap]) - { - // Swap arr[i] and arr[i+gap] - int temp = arr[i]; - arr[i] = arr[i+gap]; - arr[i+gap] = temp; - - // Set swapped - swapped = true; - } - } - } - } - - // Driver method - public static void main(String args[]) - { - CombSort ob = new CombSort(); - int arr[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0}; - ob.sort(arr); - - System.out.println("sorted array"); - for (int i=0; i> T[] sort(T arr[]) { + int size = arr.length; + + // initialize gap + int gap = size; + + // Initialize swapped as true to make sure that loop runs + boolean swapped = true; + + // Keep running while gap is more than 1 and last iteration caused a swap + while (gap != 1 || swapped) { + // Find next gap + gap = nextGap(gap); + + // Initialize swapped as false so that we can check if swap happened or not + swapped = false; + + // Compare all elements with current gap + for (int i = 0; i < size - gap ; i++) { + if (less(arr[i + gap], arr[i])) { + // Swap arr[i] and arr[i+gap] + swapped = swap(arr, i, i + gap); + } + } + } + return arr; + } + + // Driver method + public static void main(String args[]) { + CombSort ob = new CombSort(); + Integer arr[] = {8, 4, 1, 56, 3, -44, -1 , 0 , 36, 34, 8, 12 , -66, - 78, 23, -6, 28, 0}; + ob.sort(arr); + + System.out.println("sorted array"); + print(arr); + } +} diff --git a/Sorts/src/sort/SortAlgorithm.java b/Sorts/src/sort/SortAlgorithm.java index 6725d1871cfb..1a9f0437ed21 100644 --- a/Sorts/src/sort/SortAlgorithm.java +++ b/Sorts/src/sort/SortAlgorithm.java @@ -11,8 +11,18 @@ **/ public interface SortAlgorithm { + /** + * Main method arrays sorting algorithms + * @param unsorted - an array should be sorted + * @return a sorted array + */ > T[] sort(T[] unsorted); + /** + * Auxiliary method for algorithms what wanted to work with lists from JCF + * @param unsorted - a list should be sorted + * @return a sorted list + */ @SuppressWarnings("unchecked") default > List sort(List unsorted){ return Arrays.asList(sort(unsorted.toArray((T[]) new Comparable[unsorted.size()])));