I am learning Java and coding up the QuickSort
class. At some point one needs to swap elements in the array, so I am trying to do that with the Collections.swap
, which is recommended, for instance, in this question. However, javac QuickSort.java
throws an error at me:
error: swap(Object[],int,int) has private access in Collections
What am I doing wrong here? Complete code of QuickSort.java
:
package src.sort;
import java.util.Collections;
public class QuickSort {
public static void sort(Comparable[] xs) {
sort(xs, 0, xs.length);
}
private static boolean less(Comparable x, Comparable y) {
return x.compareTo(y) < 0;
}
private static void sort(Comparable[] xs, int fst, int lst) {
if (fst >= lst) return;
int i = fst, j = lst;
Comparable pivot = xs[(i + j) / 2];
while (i <= j) {
while (less(xs[i++], pivot));
while (less(pivot, xs[j--]));
if (i <= j) Collections.swap(xs, i++, j--);
}
sort(xs, fst, j);
sort(xs, i, lst);
}
}