From a84a4a29ed64c6e701859339d0c69d1cd5d4982c Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Thu, 15 Aug 2024 10:48:23 +0200 Subject: [PATCH] refactor: cleanup `InsertionSort` (#5322) --- .../thealgorithms/sorts/InsertionSort.java | 99 +++++++++---------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/InsertionSort.java b/src/main/java/com/thealgorithms/sorts/InsertionSort.java index 36aba615..21ebf382 100644 --- a/src/main/java/com/thealgorithms/sorts/InsertionSort.java +++ b/src/main/java/com/thealgorithms/sorts/InsertionSort.java @@ -1,27 +1,43 @@ package com.thealgorithms.sorts; -import java.util.function.Function; - class InsertionSort implements SortAlgorithm { /** - * Generic insertion sort algorithm in increasing order. + * Sorts the given array using the standard Insertion Sort algorithm. * - * @param array the array to be sorted. - * @param the class of array. - * @return sorted array. + * @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) { return sort(array, 0, array.length); } - public > T[] sort(T[] array, int lo, int hi) { - for (int i = lo; i < hi; i++) { - for (int j = i; j > lo && SortUtils.less(array[j], array[j - 1]); j--) { - SortUtils.swap(array, j, j - 1); - } + /** + * Sorts a subarray of the given array using the standard Insertion Sort algorithm. + * + * @param array The array to be sorted + * @param lo The starting index of the subarray + * @param hi The ending index of the subarray (exclusive) + * @param The type of elements in the array, which must be comparable + * @return The sorted array + */ + public > T[] sort(T[] array, final int lo, final int hi) { + if (array == null || lo >= hi) { + return array; } + + for (int i = lo + 1; i < hi; i++) { + final T key = array[i]; + int j = i - 1; + while (j >= lo && SortUtils.less(key, array[j])) { + array[j + 1] = array[j]; + j--; + } + array[j + 1] = key; + } + return array; } @@ -31,34 +47,25 @@ class InsertionSort implements SortAlgorithm { * comparisons like `j > 0` and swaps (we can move elements on position right, until we find * the right position for the chosen element) on further step. * - * @param array the array to be sorted - * @param Generic type which extends Comparable interface. - * @return sorted array + * @param array The array to be sorted + * @param The type of elements in the array, which must be comparable + * @return The sorted array */ public > T[] sentinelSort(T[] array) { - int minElemIndex = 0; - int n = array.length; - if (n < 1) { + if (array == null || array.length <= 1) { return array; } - // put the smallest element to the 0 position as a sentinel, which will allow us to avoid - // redundant comparisons like `j > 0` further - for (int i = 1; i < n; i++) { - if (SortUtils.less(array[i], array[minElemIndex])) { - minElemIndex = i; - } - } + final int minElemIndex = findMinIndex(array); SortUtils.swap(array, 0, minElemIndex); - for (int i = 2; i < n; i++) { + for (int i = 2; i < array.length; i++) { + final T currentValue = array[i]; int j = i; - T currentValue = array[i]; - while (SortUtils.less(currentValue, array[j - 1])) { + while (j > 0 && SortUtils.less(currentValue, array[j - 1])) { array[j] = array[j - 1]; j--; } - array[j] = currentValue; } @@ -66,29 +73,19 @@ class InsertionSort implements SortAlgorithm { } /** - * Driver Code + * Finds the index of the minimum element in the array. + * + * @param array The array to be searched + * @param The type of elements in the array, which must be comparable + * @return The index of the minimum element */ - public static void main(String[] args) { - int size = 100_000; - Double[] randomArray = SortUtilsRandomGenerator.generateArray(size); - Double[] copyRandomArray = new Double[size]; - System.arraycopy(randomArray, 0, copyRandomArray, 0, size); - - InsertionSort insertionSort = new InsertionSort(); - double insertionTime = measureApproxExecTime(insertionSort::sort, randomArray); - System.out.printf("Original insertion time: %5.2f sec.%n", insertionTime); - - double insertionSentinelTime = measureApproxExecTime(insertionSort::sentinelSort, copyRandomArray); - System.out.printf("Sentinel insertion time: %5.2f sec.%n", insertionSentinelTime); - - // ~ 1.5 time sentinel sort is faster, then classical Insertion sort implementation. - System.out.printf("Sentinel insertion is %f3.2 time faster than Original insertion sort%n", insertionTime / insertionSentinelTime); - } - - private static double measureApproxExecTime(Function sortAlgorithm, Double[] randomArray) { - long start = System.currentTimeMillis(); - sortAlgorithm.apply(randomArray); - long end = System.currentTimeMillis(); - return (end - start) / 1000.0; + private > int findMinIndex(final T[] array) { + int minIndex = 0; + for (int i = 1; i < array.length; i++) { + if (SortUtils.less(array[i], array[minIndex])) { + minIndex = i; + } + } + return minIndex; } }