我需要在我的一个类中为一个作业问题对整型数组数组进行排序。几乎每次我都会看到StackOverFlowError。我的数组是list2 [10] [10]。我的快速排序分为3种方法。 quickSort1(int,int)是主函数,分区比较一个新的分区,而swap只是简单地交换list2 [i]和list2 [j]处的整数数组。如果list2 [a]小于list2 [b],则compare(int a,int b)方法返回1,如果b小于a,则返回1,如果它们相等,则返回100。快速排序整型数组数组
我不确定我的快速排序是否正确实施,但我知道我正在交换和比较我如何说他们做的工作。我有一种预感,当我得到StackOverFlowError时,它大部分时间会永久地发生。
public static int partition(int low, int high)
{
int i = low, j = high;
int pivot = (low+high)/2;
System.out.println(i + " " + j + " " + pivot);
while (i <= j) {
while (compare(i, pivot) > 0)
i++;
while (compare(pivot, j) > 0)
j--;
if (i < j) {
swap(i,j);
i++;
j--;
}
if (i == pivot && i == j-1)
{
return i;
}
if (j == pivot && j-1 == i)
{
return i;
}
}
return i;
}
public static void quickSort1(int low, int high) {
System.out.println("Recursion: " + recursions);
int i = partition(low, high);
System.out.println(i);
if (low < i -1)
{
recursions++;
quickSort1(low, i -1);
}
if (i < high-1)
{
recursions++;
quickSort1(i, high);
}
}
public static void swap(int i, int j)
{
int[] temp = new int[n];
for(int k = 0; k < n; k++) {
temp[k] = list2[i][k];
}
for(int k = 0; k < n; k++) {
list2[i][k] = list2[j][k];
}
for(int k = 0; k < n; k++) {
list2[j][k] = temp[k];
}
}
当你说你需要理清这个数组...怎么样?是否所有的元素都需要按照排序顺序?行专业还是专业?或者只是行(或列)需要排序? – Patrick87
每次打电话给你的功能时,你是否尝试过打印一些东西? – hugomg
也许这将有助于:http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html – xagyg