1
1
package com .thealgorithms .sorts ;
2
2
3
- import java .util .function .Function ;
4
-
5
3
class InsertionSort implements SortAlgorithm {
6
4
7
5
/**
8
- * Generic insertion sort algorithm in increasing order .
6
+ * Sorts the given array using the standard Insertion Sort algorithm .
9
7
*
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
13
11
*/
14
12
@ Override
15
13
public <T extends Comparable <T >> T [] sort (T [] array ) {
16
14
return sort (array , 0 , array .length );
17
15
}
18
16
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 --;
23
37
}
38
+ array [j + 1 ] = key ;
24
39
}
40
+
25
41
return array ;
26
42
}
27
43
@@ -31,64 +47,45 @@ public <T extends Comparable<T>> T[] sort(T[] array, int lo, int hi) {
31
47
* comparisons like `j > 0` and swaps (we can move elements on position right, until we find
32
48
* the right position for the chosen element) on further step.
33
49
*
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
37
53
*/
38
54
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 ) {
42
56
return array ;
43
57
}
44
58
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 );
52
60
SortUtils .swap (array , 0 , minElemIndex );
53
61
54
- for (int i = 2 ; i < n ; i ++) {
62
+ for (int i = 2 ; i < array .length ; i ++) {
63
+ final T currentValue = array [i ];
55
64
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 ])) {
58
66
array [j ] = array [j - 1 ];
59
67
j --;
60
68
}
61
-
62
69
array [j ] = currentValue ;
63
70
}
64
71
65
72
return array ;
66
73
}
67
74
68
75
/**
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
70
81
*/
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 ;
93
90
}
94
91
}
0 commit comments