Modify Insertion sort implementation to classical one + add function insertion-sentinel sort. (#3622)

* bug fix for CircularBuffer + refactoring + add unit tests

* change Insertion sort to classical implementation + add isSorted function to SortUtils + add SortUtilsRandomGenerator for generating random values and arrays

* little fix

Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
Narek Karapetian
2022-11-06 04:24:08 -08:00
committed by GitHub
parent c8ecd23183
commit 58cf08f2fd
6 changed files with 303 additions and 20 deletions

View File

@ -1,7 +1,8 @@
package com.thealgorithms.sorts;
import static com.thealgorithms.sorts.SortUtils.less;
import static com.thealgorithms.sorts.SortUtils.print;
import java.util.function.Function;
import static com.thealgorithms.sorts.SortUtils.*;
class InsertionSort implements SortAlgorithm {
@ -9,21 +10,51 @@ class InsertionSort implements SortAlgorithm {
* Generic insertion sort algorithm in increasing order.
*
* @param array the array to be sorted.
* @param <T> the class of array.
* @param <T> the class of array.
* @return sorted array.
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
for (int i = 1; i < array.length; i++) {
T insertValue = array[i];
int j;
for (j = i - 1; j >= 0 && less(insertValue, array[j]); j--) {
array[j + 1] = array[j];
}
if (j != i - 1) {
array[j + 1] = insertValue;
for (int i = 1; i < array.length; i++)
for (int j = i; j > 0 && less(array[j], array[j - 1]); j--)
swap(array, j, j - 1);
return array;
}
/**
* Sentinel sort is a function which on the first step finds the minimal element in the provided
* array and puts it to the zero position, such a trick gives us an ability to avoid redundant
* 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 <T> Generic type which extends Comparable interface.
* @return sorted array
*/
public <T extends Comparable<T>> T[] sentinelSort(T[] array) {
int minElemIndex = 0;
int n = array.length;
if (n < 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 (less(array[i], array[minElemIndex]))
minElemIndex = i;
swap(array, 0, minElemIndex);
for (int i = 2; i < n; i++) {
int j = i;
T currentValue = array[i];
while (less(currentValue, array[j - 1])) {
array[j] = array[j - 1];
j--;
}
array[j] = currentValue;
}
return array;
}
@ -31,15 +62,27 @@ class InsertionSort implements SortAlgorithm {
* Driver Code
*/
public static void main(String[] args) {
Integer[] integers = { 4, 23, 6, 78, 1, 54, 231, 9, 12 };
InsertionSort sort = new InsertionSort();
sort.sort(integers);
print(integers);
/* [1, 4, 6, 9, 12, 23, 54, 78, 231] */
int size = 100_000;
Double[] randomArray = SortUtilsRandomGenerator.generateArray(size);
Double[] copyRandomArray = new Double[size];
System.arraycopy(randomArray, 0, copyRandomArray, 0, size);
String[] strings = { "c", "a", "e", "b", "d" };
sort.sort(strings);
print(strings);
/* [a, b, c, d, e] */
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<Double[], Double[]> sortAlgorithm, Double[] randomArray) {
long start = System.currentTimeMillis();
sortAlgorithm.apply(randomArray);
long end = System.currentTimeMillis();
return (end - start) / 1000.0;
}
}

View File

@ -99,4 +99,17 @@ final class SortUtils {
swap(array, left++, right--);
}
}
/**
* Function to check if the array is sorted. By default, it will check if the array is sorted in ASC order.
*
* @param array - an array which to check is it sorted or not.
* @return true - if array sorted in ASC order, false otherwise.
*/
static <T extends Comparable<T>> boolean isSorted(T[] array) {
for (int i = 1; i < array.length; i++)
if (less(array[i], array[i - 1]))
return false;
return true;
}
}

View File

@ -0,0 +1,38 @@
package com.thealgorithms.sorts;
import java.util.Random;
public class SortUtilsRandomGenerator {
private static final Random random;
private static final long seed;
static {
seed = System.currentTimeMillis();
random = new Random(seed);
}
/**
* Function to generate array of double values, with predefined size.
*
* @param size result array size
* @return array of Double values, randomly generated, each element is between [0, 1)
*/
public static Double[] generateArray(int size) {
Double[] arr = new Double[size];
for (int i = 0; i < size; i++)
arr[i] = generateDouble();
return arr;
}
/**
* Function to generate Double value.
*
* @return Double value [0, 1)
*/
public static Double generateDouble() {
return random.nextDouble();
}
}