From 7070f802bd4bc1e2480e55b41de458f19ac35108 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sat, 22 Jun 2024 14:15:30 +0200 Subject: [PATCH 1/2] Refactor: Replace Swap and Comparison Methods with SortUtils Utility Methods --- .../com/thealgorithms/sorts/HeapSort.java | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/HeapSort.java b/src/main/java/com/thealgorithms/sorts/HeapSort.java index eec705ba476a..9fc569cef720 100644 --- a/src/main/java/com/thealgorithms/sorts/HeapSort.java +++ b/src/main/java/com/thealgorithms/sorts/HeapSort.java @@ -9,16 +9,17 @@ public class HeapSort implements SortAlgorithm { /** * For simplicity, we are considering the heap root index as 1 instead of 0. - * It simplifies future calculations. Because of that we are decreasing the - * provided indexes by 1 in {@link #swap(Object[], int, int)} and - * {@link #less(Comparable[], int, int)} functions. + * This approach simplifies future calculations. As a result, we decrease + * the indexes by 1 when calling {@link SortUtils#less(Comparable, Comparable)} + * and provide adjusted values to the {@link SortUtils#swap(Object[], int, int)} methods. */ @Override public > T[] sort(T[] unsorted) { int n = unsorted.length; heapify(unsorted, n); while (n > 1) { - swap(unsorted, 1, n--); + SortUtils.swap(unsorted, 0, n - 1); + n--; siftDown(unsorted, 1, n); } return unsorted; @@ -33,24 +34,14 @@ private static > void heapify(T[] unsorted, int n) { private static > void siftDown(T[] unsorted, int k, int n) { while (2 * k <= n) { int j = 2 * k; - if (j < n && less(unsorted, j, j + 1)) { + if (j < n && SortUtils.less(unsorted[j - 1], unsorted[j])) { j++; } - if (!less(unsorted, k, j)) { + if (!SortUtils.less(unsorted[k - 1], unsorted[j - 1])) { break; } - swap(unsorted, k, j); + SortUtils.swap(unsorted, k - 1, j - 1); k = j; } } - - private static void swap(T[] array, int idx, int idy) { - T swap = array[idx - 1]; - array[idx - 1] = array[idy - 1]; - array[idy - 1] = swap; - } - - private static > boolean less(T[] array, int idx, int idy) { - return array[idx - 1].compareTo(array[idy - 1]) < 0; - } } From 112aabd677bc7647d48259aaa36774d41f1f13fe Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sat, 22 Jun 2024 14:17:44 +0200 Subject: [PATCH 2/2] Rename parameter unsorted to array --- .../com/thealgorithms/sorts/HeapSort.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/HeapSort.java b/src/main/java/com/thealgorithms/sorts/HeapSort.java index 9fc569cef720..91d556b17b16 100644 --- a/src/main/java/com/thealgorithms/sorts/HeapSort.java +++ b/src/main/java/com/thealgorithms/sorts/HeapSort.java @@ -14,33 +14,33 @@ public class HeapSort implements SortAlgorithm { * and provide adjusted values to the {@link SortUtils#swap(Object[], int, int)} methods. */ @Override - public > T[] sort(T[] unsorted) { - int n = unsorted.length; - heapify(unsorted, n); + public > T[] sort(T[] array) { + int n = array.length; + heapify(array, n); while (n > 1) { - SortUtils.swap(unsorted, 0, n - 1); + SortUtils.swap(array, 0, n - 1); n--; - siftDown(unsorted, 1, n); + siftDown(array, 1, n); } - return unsorted; + return array; } - private static > void heapify(T[] unsorted, int n) { + private static > void heapify(T[] array, int n) { for (int k = n / 2; k >= 1; k--) { - siftDown(unsorted, k, n); + siftDown(array, k, n); } } - private static > void siftDown(T[] unsorted, int k, int n) { + private static > void siftDown(T[] array, int k, int n) { while (2 * k <= n) { int j = 2 * k; - if (j < n && SortUtils.less(unsorted[j - 1], unsorted[j])) { + if (j < n && SortUtils.less(array[j - 1], array[j])) { j++; } - if (!SortUtils.less(unsorted[k - 1], unsorted[j - 1])) { + if (!SortUtils.less(array[k - 1], array[j - 1])) { break; } - SortUtils.swap(unsorted, k - 1, j - 1); + SortUtils.swap(array, k - 1, j - 1); k = j; } }