Renamed method variables (#7296)

* renamed method variables for improved readibility in the sort method of InsertionSort

* Renamed method in PancackeSort.

* Renamed array "aux" to "tempArray"

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
This commit is contained in:
Debojeet Bhattacharya
2026-03-08 09:27:33 -05:00
committed by GitHub
parent 8b41533d00
commit 9aa2544f61
3 changed files with 25 additions and 25 deletions

View File

@@ -33,30 +33,30 @@ class InsertionSort implements SortAlgorithm {
}
/**
* 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;
}
/**

View File

@@ -10,7 +10,7 @@ import static com.thealgorithms.sorts.SortUtils.less;
@SuppressWarnings("rawtypes")
class MergeSort implements SortAlgorithm {
private Comparable[] aux;
private Comparable[] tempArray;
/**
* Generic merge sort algorithm.
@@ -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;
}
@@ -58,17 +58,17 @@ class MergeSort implements SortAlgorithm {
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++];
}
}
}

View File

@@ -15,7 +15,7 @@ public class PancakeSort implements SortAlgorithm {
}
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);
}
@@ -30,7 +30,7 @@ public class PancakeSort implements SortAlgorithm {
* @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++) {