Skip to content

Commit 9aa2544

Browse files
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>
1 parent 8b41533 commit 9aa2544

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

src/main/java/com/thealgorithms/sorts/InsertionSort.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,30 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
3333
}
3434

3535
/**
36-
* Sorts a subarray of the given array using the standard Insertion Sort algorithm.
36+
* Sorts a subarray of the given items using the standard Insertion Sort algorithm.
3737
*
38-
* @param array The array to be sorted
39-
* @param lo The starting index of the subarray
40-
* @param hi The ending index of the subarray (exclusive)
41-
* @param <T> The type of elements in the array, which must be comparable
42-
* @return The sorted array
38+
* @param items The items to be sorted
39+
* @param startIndex The starting index of the subarray
40+
* @param endIndex The ending index of the subarray (exclusive)
41+
* @param <T> The type of elements in the items, which must be comparable
42+
* @return The sorted items
4343
*/
44-
public <T extends Comparable<T>> T[] sort(T[] array, final int lo, final int hi) {
45-
if (array == null || lo >= hi) {
46-
return array;
44+
public <T extends Comparable<T>> T[] sort(T[] items, final int startIndex, final int endIndex) {
45+
if (items == null || startIndex >= endIndex) {
46+
return items;
4747
}
4848

49-
for (int i = lo + 1; i < hi; i++) {
50-
final T key = array[i];
49+
for (int i = startIndex + 1; i < endIndex; i++) {
50+
final T key = items[i];
5151
int j = i - 1;
52-
while (j >= lo && SortUtils.less(key, array[j])) {
53-
array[j + 1] = array[j];
52+
while (j >= startIndex && SortUtils.less(key, items[j])) {
53+
items[j + 1] = items[j];
5454
j--;
5555
}
56-
array[j + 1] = key;
56+
items[j + 1] = key;
5757
}
5858

59-
return array;
59+
return items;
6060
}
6161

6262
/**

src/main/java/com/thealgorithms/sorts/MergeSort.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@SuppressWarnings("rawtypes")
1111
class MergeSort implements SortAlgorithm {
1212

13-
private Comparable[] aux;
13+
private Comparable[] tempArray;
1414

1515
/**
1616
* Generic merge sort algorithm.
@@ -26,7 +26,7 @@ class MergeSort implements SortAlgorithm {
2626
*/
2727
@Override
2828
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
29-
aux = new Comparable[unsorted.length];
29+
tempArray = new Comparable[unsorted.length];
3030
doSort(unsorted, 0, unsorted.length - 1);
3131
return unsorted;
3232
}
@@ -58,17 +58,17 @@ private <T extends Comparable<T>> void doSort(T[] arr, int left, int right) {
5858
private <T extends Comparable<T>> void merge(T[] arr, int left, int mid, int right) {
5959
int i = left;
6060
int j = mid + 1;
61-
System.arraycopy(arr, left, aux, left, right + 1 - left);
61+
System.arraycopy(arr, left, tempArray, left, right + 1 - left);
6262

6363
for (int k = left; k <= right; k++) {
6464
if (j > right) {
65-
arr[k] = (T) aux[i++];
65+
arr[k] = (T) tempArray[i++];
6666
} else if (i > mid) {
67-
arr[k] = (T) aux[j++];
68-
} else if (less(aux[j], aux[i])) {
69-
arr[k] = (T) aux[j++];
67+
arr[k] = (T) tempArray[j++];
68+
} else if (less(tempArray[j], tempArray[i])) {
69+
arr[k] = (T) tempArray[j++];
7070
} else {
71-
arr[k] = (T) aux[i++];
71+
arr[k] = (T) tempArray[i++];
7272
}
7373
}
7474
}

src/main/java/com/thealgorithms/sorts/PancakeSort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
1515
}
1616

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

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

0 commit comments

Comments
 (0)