3310487509
22.11.2022, aktualisiert am 02.12.2022
4838
2
1
The swap method takes an array and two integer array indices as arguments
The code below is for a rapid sort class. There is a function named swap near the bottom that is intended to accept an array and swap two of the integers in the array. Is this something that Java can do? Is it feasible to have a method in the format swap(T, int, int) that will function? I think my question is clear.
public class QuickSort {
public static <T extends Comparable<T>> void sort(T table) {
quickSort(table, 0, table.length - 1);
}
private static <T extends Comparable<T>> void quickSort(T table, int first, int last) {
if (first < last) {
int pivIndex = partition(table, first, last);
quickSort(table, first, pivIndex - 1);
quickSort(table, pivIndex + 1, last);
}
}
private static <T extends Comparable<T>> int partition(T table, int first, int last) {
T pivot = table[first];
int up = first;
int down = last;
do {
while ((up < last) && (pivot.compareTo(table[up]) >= 0)) {
up++;
}
while (pivot.compareTo(table[down]) < 0) {
down--;
}
if (up < down) {
swap(table, up, down);
}
}
while (up < down);
swap(table, first, down);
return down;
}
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 4720234988
Url: https://administrator.de/contentid/4720234988
Ausgedruckt am: 24.11.2024 um 10:11 Uhr
2 Kommentare
Neuester Kommentar