我正在研究快速排序算法。我不确定我要去哪里。我相信我已经正确实现了该算法,因为它可以对某些随机值进行排序,而对于其他随机值则不能。尝试调试此问题时,我已将数组的大小更改为3,而不是初始的20。这是我当前的实现:
public class Quicksort {
public static void main(String[] args) {
// Generate a random array of integers to sort
// Integer[] numbers = new Integer[20];
// testing int, above is original
Integer[] numbers = new Integer[3];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (int) (Math.random() * 100);
}
// Print unsorted array
printArray(numbers);
// Sort array
quicksort(numbers);
// Print sorted array
printArray(numbers);
}
// Print a comma-separated list of the elements in array
public static <T> void printArray(T[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + (i < array.length - 1 ? ", " : "\n"));
}
}
// Print a comma-separated list of the elements in array drawing attention to
// the pivot and bounds of the partition
public static <T> void printArrayPartition(T[] array, int low, int high, int pivot) {
for (int i = 0; i < array.length; i++) {
String element = "" + array[i];
if (i == pivot)
element = "*" + element + "*";
if (i == low)
element = "[" + element;
if (i == high)
element += "]";
System.out.print(element + (i < array.length - 1 ? ", " : "\n"));
}
}
// This is a shortcut to call the recursive quicksort on the entire array
public static <T extends Comparable<T>> void quicksort(T[] array) {
quicksort(array, 0, array.length - 1);
}
// Recursively quicksort a partition of the array
private static <T extends Comparable<T>> void quicksort(T[] array, int low, int high) {
// If high is greater than low, then there are at least two elements in this
// partition and it needs to be sorted. Otherwise it contains at most 1 element
// and is therefore already sorted.
if (low < high) {
int pivotIndex = partition(array, low, high);
// Uncomment printArrayPartition to print the array after every partition
// (helpful for debugging). Every element within the partition (enclosed by [])
// should appear on the correct side of the pivot value (marked by **)
printArrayPartition(array, low, high, pivotIndex);
// Quicksort the left and right partitions
quicksort(array, low, pivotIndex - 1);
quicksort(array, pivotIndex + 1, high);
}
}
// Split a partition of the array into two new partitions and one pivot. The
// left partition includes everything less than the pivot, and the right
// partition is everything greater than or equal to the pivot (excluding the
// pivot, which remains between the two partitions). Returns the index of the
// pivot.
private static <T extends Comparable<T>> int partition(T[] array, int low, int high) {
// Use the last element of the partition as the pivot
T pivotValue = array[high];
//int pivot = (int) pivotValue;
// This marks the index AFTER the end of the (new) left partition. Right now
// there is no left partition, so it's at the beginning of the partition we're
// currently splitting up.
int left = low;
// For each element in the partition (except the pivot)
for (int i = 0; i < array.length; i++) {
// Compare the current element to the pivot (high)
if (array[i].compareTo(pivotValue) < 0) {
// If the current element is less than the pivot, swap it with the leftmost
// unpartitioned element (making it part of the left partition) and update the
// left partition
swap(array, left, i);
left++;
}
}
// Swap the pivot with the leftmost unpartitioned element. The unpartitioned
// elements are now the right partition.
swap(array, high, left);
// Return the new index of the pivot
return left;
}
// Swap the elements of array at index a and index b
private static <T> void swap(T[] array, int a, int b) {
T temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}
我收到编译错误消息:
58, 89, 53
[*53*, 89, 58]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Quicksort.swap(Quicksort.java:103)
at Quicksort.partition(Quicksort.java:89)
at Quicksort.quicksort(Quicksort.java:54)
at Quicksort.quicksort(Quicksort.java:62)
at Quicksort.quicksort(Quicksort.java:45)
at Quicksort.main(Quicksort.java:15)
我看不到问题。任何帮助是极大的赞赏。
答案 0 :(得分:0)
您在partition()
中的界限不正确。
for (int i = 0; i < array.length; i++)
您应该遍历实际分区的一部分:
for (int i = low; i < high; i++)