Skip to content

Commit a84a4a2

Browse files
authored
refactor: cleanup InsertionSort (#5322)
1 parent 046f5a4 commit a84a4a2

File tree

1 file changed

+47
-50
lines changed

1 file changed

+47
-50
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
package com.thealgorithms.sorts;
22

3-
import java.util.function.Function;
4-
53
class InsertionSort implements SortAlgorithm {
64

75
/**
8-
* Generic insertion sort algorithm in increasing order.
6+
* Sorts the given array using the standard Insertion Sort algorithm.
97
*
10-
* @param array the array to be sorted.
11-
* @param <T> the class of array.
12-
* @return sorted array.
8+
* @param array The array to be sorted
9+
* @param <T> The type of elements in the array, which must be comparable
10+
* @return The sorted array
1311
*/
1412
@Override
1513
public <T extends Comparable<T>> T[] sort(T[] array) {
1614
return sort(array, 0, array.length);
1715
}
1816

19-
public <T extends Comparable<T>> T[] sort(T[] array, int lo, int hi) {
20-
for (int i = lo; i < hi; i++) {
21-
for (int j = i; j > lo && SortUtils.less(array[j], array[j - 1]); j--) {
22-
SortUtils.swap(array, j, j - 1);
17+
/**
18+
* Sorts a subarray of the given array using the standard Insertion Sort algorithm.
19+
*
20+
* @param array The array to be sorted
21+
* @param lo The starting index of the subarray
22+
* @param hi The ending index of the subarray (exclusive)
23+
* @param <T> The type of elements in the array, which must be comparable
24+
* @return The sorted array
25+
*/
26+
public <T extends Comparable<T>> T[] sort(T[] array, final int lo, final int hi) {
27+
if (array == null || lo >= hi) {
28+
return array;
29+
}
30+
31+
for (int i = lo + 1; i < hi; i++) {
32+
final T key = array[i];
33+
int j = i - 1;
34+
while (j >= lo && SortUtils.less(key, array[j])) {
35+
array[j + 1] = array[j];
36+
j--;
2337
}
38+
array[j + 1] = key;
2439
}
40+
2541
return array;
2642
}
2743

@@ -31,64 +47,45 @@ public <T extends Comparable<T>> T[] sort(T[] array, int lo, int hi) {
3147
* comparisons like `j > 0` and swaps (we can move elements on position right, until we find
3248
* the right position for the chosen element) on further step.
3349
*
34-
* @param array the array to be sorted
35-
* @param <T> Generic type which extends Comparable interface.
36-
* @return sorted array
50+
* @param array The array to be sorted
51+
* @param <T> The type of elements in the array, which must be comparable
52+
* @return The sorted array
3753
*/
3854
public <T extends Comparable<T>> T[] sentinelSort(T[] array) {
39-
int minElemIndex = 0;
40-
int n = array.length;
41-
if (n < 1) {
55+
if (array == null || array.length <= 1) {
4256
return array;
4357
}
4458

45-
// put the smallest element to the 0 position as a sentinel, which will allow us to avoid
46-
// redundant comparisons like `j > 0` further
47-
for (int i = 1; i < n; i++) {
48-
if (SortUtils.less(array[i], array[minElemIndex])) {
49-
minElemIndex = i;
50-
}
51-
}
59+
final int minElemIndex = findMinIndex(array);
5260
SortUtils.swap(array, 0, minElemIndex);
5361

54-
for (int i = 2; i < n; i++) {
62+
for (int i = 2; i < array.length; i++) {
63+
final T currentValue = array[i];
5564
int j = i;
56-
T currentValue = array[i];
57-
while (SortUtils.less(currentValue, array[j - 1])) {
65+
while (j > 0 && SortUtils.less(currentValue, array[j - 1])) {
5866
array[j] = array[j - 1];
5967
j--;
6068
}
61-
6269
array[j] = currentValue;
6370
}
6471

6572
return array;
6673
}
6774

6875
/**
69-
* Driver Code
76+
* Finds the index of the minimum element in the array.
77+
*
78+
* @param array The array to be searched
79+
* @param <T> The type of elements in the array, which must be comparable
80+
* @return The index of the minimum element
7081
*/
71-
public static void main(String[] args) {
72-
int size = 100_000;
73-
Double[] randomArray = SortUtilsRandomGenerator.generateArray(size);
74-
Double[] copyRandomArray = new Double[size];
75-
System.arraycopy(randomArray, 0, copyRandomArray, 0, size);
76-
77-
InsertionSort insertionSort = new InsertionSort();
78-
double insertionTime = measureApproxExecTime(insertionSort::sort, randomArray);
79-
System.out.printf("Original insertion time: %5.2f sec.%n", insertionTime);
80-
81-
double insertionSentinelTime = measureApproxExecTime(insertionSort::sentinelSort, copyRandomArray);
82-
System.out.printf("Sentinel insertion time: %5.2f sec.%n", insertionSentinelTime);
83-
84-
// ~ 1.5 time sentinel sort is faster, then classical Insertion sort implementation.
85-
System.out.printf("Sentinel insertion is %f3.2 time faster than Original insertion sort%n", insertionTime / insertionSentinelTime);
86-
}
87-
88-
private static double measureApproxExecTime(Function<Double[], Double[]> sortAlgorithm, Double[] randomArray) {
89-
long start = System.currentTimeMillis();
90-
sortAlgorithm.apply(randomArray);
91-
long end = System.currentTimeMillis();
92-
return (end - start) / 1000.0;
82+
private <T extends Comparable<T>> int findMinIndex(final T[] array) {
83+
int minIndex = 0;
84+
for (int i = 1; i < array.length; i++) {
85+
if (SortUtils.less(array[i], array[minIndex])) {
86+
minIndex = i;
87+
}
88+
}
89+
return minIndex;
9390
}
9491
}

0 commit comments

Comments
 (0)