Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/main/java/com/thealgorithms/sorts/InsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
}

/**
* Sorts a subarray of the given array using the standard Insertion Sort algorithm.
* Sorts a subarray of the given items 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 <T> The type of elements in the array, which must be comparable
* @return The sorted array
* @param items The items to be sorted
* @param startIndex The starting index of the subarray
* @param endIndex The ending index of the subarray (exclusive)
* @param <T> The type of elements in the items, which must be comparable
* @return The sorted items
*/
public <T extends Comparable<T>> T[] sort(T[] array, final int lo, final int hi) {
if (array == null || lo >= hi) {
return array;
public <T extends Comparable<T>> T[] sort(T[] items, final int startIndex, final int endIndex) {
if (items == null || startIndex >= endIndex) {
return items;
}

for (int i = lo + 1; i < hi; i++) {
final T key = array[i];
for (int i = startIndex + 1; i < endIndex; i++) {
final T key = items[i];
int j = i - 1;
while (j >= lo && SortUtils.less(key, array[j])) {
array[j + 1] = array[j];
while (j >= startIndex && SortUtils.less(key, items[j])) {
items[j + 1] = items[j];
j--;
}
array[j + 1] = key;
items[j + 1] = key;
}

return array;
return items;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/thealgorithms/sorts/MergeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@SuppressWarnings("rawtypes")
class MergeSort implements SortAlgorithm {

private Comparable[] aux;
private Comparable[] tempArray;

/**
* Generic merge sort algorithm.
Expand All @@ -26,7 +26,7 @@ class MergeSort implements SortAlgorithm {
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
aux = new Comparable[unsorted.length];
tempArray = new Comparable[unsorted.length];
doSort(unsorted, 0, unsorted.length - 1);
return unsorted;
}
Expand Down Expand Up @@ -58,17 +58,17 @@ private <T extends Comparable<T>> void doSort(T[] arr, int left, int right) {
private <T extends Comparable<T>> void merge(T[] arr, int left, int mid, int right) {
int i = left;
int j = mid + 1;
System.arraycopy(arr, left, aux, left, right + 1 - left);
System.arraycopy(arr, left, tempArray, left, right + 1 - left);

for (int k = left; k <= right; k++) {
if (j > right) {
arr[k] = (T) aux[i++];
arr[k] = (T) tempArray[i++];
} else if (i > mid) {
arr[k] = (T) aux[j++];
} else if (less(aux[j], aux[i])) {
arr[k] = (T) aux[j++];
arr[k] = (T) tempArray[j++];
} else if (less(tempArray[j], tempArray[i])) {
arr[k] = (T) tempArray[j++];
} else {
arr[k] = (T) aux[i++];
arr[k] = (T) tempArray[i++];
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/sorts/PancakeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
}

for (int currentSize = 0; currentSize < array.length; currentSize++) {
int maxIndex = findMaxIndex(array, currentSize);
int maxIndex = findIndexOfMax(array, currentSize);
SortUtils.flip(array, maxIndex, array.length - 1 - currentSize);
}

Expand All @@ -30,7 +30,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
* @param <T> the type of elements in the array
* @return the index of the maximum element
*/
private <T extends Comparable<T>> int findMaxIndex(T[] array, int currentSize) {
private <T extends Comparable<T>> int findIndexOfMax(T[] array, int currentSize) {
T max = array[0];
int maxIndex = 0;
for (int i = 0; i < array.length - currentSize; i++) {
Expand Down
Loading