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-Key: 4720234988

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

Printed on: April 20, 2024 at 03:04 o'clock

Mitglied: 4400667902
Solution 4400667902 Nov 22, 2022 at 14:03:45 (UTC)
Goto Top
Mitglied: 3310487509
3310487509 Nov 30, 2022 at 13:32:14 (UTC)
Goto Top

Thanks for the help! It really help.