From 1cf6847373dd311df0fc99051a317f28169c26fb Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 23 Jun 2024 14:15:32 +0200 Subject: [PATCH 1/2] Fix Swap Sort Implementation: Correct Logic and Improve Efficiency --- .../com/thealgorithms/sorts/SwapSort.java | 63 ++++--------------- .../com/thealgorithms/sorts/SwapSortTest.java | 8 +++ 2 files changed, 20 insertions(+), 51 deletions(-) create mode 100644 src/test/java/com/thealgorithms/sorts/SwapSortTest.java diff --git a/src/main/java/com/thealgorithms/sorts/SwapSort.java b/src/main/java/com/thealgorithms/sorts/SwapSort.java index e0fa7087a49e..7ce953396209 100644 --- a/src/main/java/com/thealgorithms/sorts/SwapSort.java +++ b/src/main/java/com/thealgorithms/sorts/SwapSort.java @@ -9,64 +9,25 @@ */ public class SwapSort implements SortAlgorithm { + /** + * Sorts the input array using the swap sort algorithm. + * + * @param array the array to be sorted + * @param the type of elements in the array, which must be Comparable + * @return the sorted array + */ @Override public > T[] sort(T[] array) { int len = array.length; - int index = 0; - while (index < len - 1) { - int amountSmallerElements = this.getSmallerElementCount(array, index); - - if (amountSmallerElements > 0 && index != amountSmallerElements) { - SortUtils.swap(array, index, amountSmallerElements); - } else { - index++; + for (int i = 0; i < len - 1; i++) { + for (int j = i + 1; j < len; j++) { + if (SortUtils.less(array[j], array[i])) { + SortUtils.swap(array, i, j); + } } } return array; } - - private > int getSmallerElementCount(T[] array, int index) { - int counter = 0; - for (int i = 0; i < array.length; i++) { - if (SortUtils.less(array[i], array[index])) { - counter++; - } - } - - return counter; - } - - public static void main(String[] args) { - // ==== Int ======= - Integer[] a = {3, 7, 45, 1, 33, 5, 2, 9}; - System.out.print("unsorted: "); - SortUtils.print(a); - System.out.println(); - - new SwapSort().sort(a); - System.out.print("sorted: "); - SortUtils.print(a); - System.out.println(); - - // ==== String ======= - String[] b = { - "banana", - "berry", - "orange", - "grape", - "peach", - "cherry", - "apple", - "pineapple", - }; - System.out.print("unsorted: "); - SortUtils.print(b); - System.out.println(); - - new SwapSort().sort(b); - System.out.print("sorted: "); - SortUtils.print(b); - } } diff --git a/src/test/java/com/thealgorithms/sorts/SwapSortTest.java b/src/test/java/com/thealgorithms/sorts/SwapSortTest.java new file mode 100644 index 000000000000..c1638a385940 --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/SwapSortTest.java @@ -0,0 +1,8 @@ +package com.thealgorithms.sorts; + +public class SwapSortTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new SwapSort(); + } +} From 06770a20cca27e0b0494fab657139d4f38b8305e Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 23 Jun 2024 23:53:49 +0200 Subject: [PATCH 2/2] Adjusting fix for swap sort to another variant --- .../com/thealgorithms/sorts/SwapSort.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/SwapSort.java b/src/main/java/com/thealgorithms/sorts/SwapSort.java index 7ce953396209..fe3597c0e2b4 100644 --- a/src/main/java/com/thealgorithms/sorts/SwapSort.java +++ b/src/main/java/com/thealgorithms/sorts/SwapSort.java @@ -9,25 +9,32 @@ */ public class SwapSort implements SortAlgorithm { - /** - * Sorts the input array using the swap sort algorithm. - * - * @param array the array to be sorted - * @param the type of elements in the array, which must be Comparable - * @return the sorted array - */ @Override public > T[] sort(T[] array) { int len = array.length; + int index = 0; - for (int i = 0; i < len - 1; i++) { - for (int j = i + 1; j < len; j++) { - if (SortUtils.less(array[j], array[i])) { - SortUtils.swap(array, i, j); - } + while (index < len - 1) { + int amountSmallerElements = this.getSmallerElementCount(array, index); + + if (amountSmallerElements > 0) { + SortUtils.swap(array, index, index + amountSmallerElements); + } else { + index++; } } return array; } + + private > int getSmallerElementCount(T[] array, int index) { + int counter = 0; + for (int i = index + 1; i < array.length; i++) { + if (SortUtils.less(array[i], array[index])) { + counter++; + } + } + + return counter; + } }