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