3310487509
Goto Top

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;
}

Content-ID: 4720234988

Url: https://administrator.de/contentid/4720234988

Ausgedruckt am: 24.11.2024 um 10:11 Uhr

4400667902
Lösung 4400667902 22.11.2022 um 15:03:45 Uhr
Goto Top
3310487509
3310487509 30.11.2022 um 14:32:14 Uhr
Goto Top

Thanks for the help! It really help.