Java 递归分区排序无效
我已经为分区排序编写了一个递归方法,对数组进行排序,但是当我使用一个包含10-20个元素的数组时,程序需要很长时间才能完成(在我的计算机上,100000 int数组的冒泡排序大约需要15-20秒,但对于仅30 int的数组,我的分区排序大约需要45秒进行排序 这是代码Java 递归分区排序无效,java,sorting,quicksort,Java,Sorting,Quicksort,我已经为分区排序编写了一个递归方法,对数组进行排序,但是当我使用一个包含10-20个元素的数组时,程序需要很长时间才能完成(在我的计算机上,100000 int数组的冒泡排序大约需要15-20秒,但对于仅30 int的数组,我的分区排序大约需要45秒进行排序 这是代码 public static int[] partitionSortRecursive(int[] array, int beginning, int end) { if (end < beginning)
public static int[] partitionSortRecursive(int[] array, int beginning, int end)
{
if (end < beginning)
return array;
int pivot = (array[beginning] + array[end]) / 2;
int firstUnknown = beginning;
int lastS1 = beginning - 1;
int firstS3 = end + 1;
while (firstUnknown < firstS3)
{
if (array[firstUnknown] == pivot)
{
firstUnknown++;
}
else if (array[firstUnknown] > pivot)
{
firstS3--;
int temp = array[firstUnknown];
array[firstUnknown] = array[firstS3];
array[firstS3] = temp;
}
else
{
lastS1++;
int temp = array[firstUnknown];
array[firstUnknown] = array[lastS1];
array[lastS1] = temp;
firstUnknown++;
}
}
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
return array;
}
public static int[]partitionSortRecursive(int[]数组,int开头,int结尾)
{
如果(结束<开始)
返回数组;
int pivot=(数组[开始]+数组[结束])/2;
int firstUnknown=开始;
int lastS1=开始-1;
int firstS3=结束+1;
while(firstUnknownpivot)
{
第一个3--;
int temp=数组[firstUnknown];
数组[firstUnknown]=数组[firstS3];
数组[firstS3]=temp;
}
其他的
{
lastS1++;
int temp=数组[firstUnknown];
数组[firstUnknown]=数组[lastS1];
数组[lastS1]=temp;
firstUnknown++;
}
}
partitionSortRecursive(数组,0,lastS1);
partitionSortRecursive(数组,firstS3,end);
返回数组;
}
而不是像这样的直接资源调用
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
在可以保存索引对的地方组织内部堆栈。当堆栈不为空时,从堆栈中获取下一对。在函数末尾,不要调用同一个函数,而是在堆栈中放入2对
(0,lastS1)
和(firstS3,end)
而不是像这样的直接重新调用
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
在可以保存索引对的地方组织内部堆栈。当堆栈不为空时,从堆栈中获取下一对。在函数末尾,不要调用同一个函数,而是在堆栈中放入2对
(0,lastS1)
和(firstS3,end)
而不是像这样的直接重新调用
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
在可以保存索引对的地方组织内部堆栈。当堆栈不为空时,从堆栈中获取下一对。在函数末尾,不要调用同一个函数,而是在堆栈中放入2对
(0,lastS1)
和(firstS3,end)
而不是像这样的直接重新调用
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
在可以保存索引对的地方组织内部堆栈。当堆栈不为空时,从堆栈中获取下一对。在函数末尾,不要调用同一个函数,而是在堆栈中放入2对
(0,lastS1)
和(firstS3,end)
您没有使用正确的pivot元素。您需要计算左右值的平均值,但必须从子数组中提取一个样本值以进行分区
你可以选择最右边的,中间的或者任何其他的元素,所以你的第一行代码应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
您还可以选择任何其他元素(例如随机)
编辑:这并不能解决性能问题
据我所知,快速排序将把一个数组分成两个子数组A和B,其中A中的所有元素都小于B中的任何元素,然后对这两个子数组执行相同的操作
所以基本的调用结构应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
把你的实现基本上是
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, 0, pivot) // <<<<<< notice the '0' instead of 'i'
DoSort (array, pivot + 1, j)
}
void DoSort(数组,i,j)
{
pivot=分区(数组,i,j)
DoSort(array,0,pivot)/您没有使用正确的pivot元素。您计算左值和右值的平均值,但必须从子数组中获取一个样本值以进行分区
你可以选择最右边的,中间的或者任何其他的元素,所以你的第一行代码应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
您还可以选择任何其他元素(例如随机)
编辑:这并不能解决性能问题
据我所知,快速排序将把一个数组分成两个子数组A和B,其中A中的所有元素都小于B中的任何元素,然后对这两个子数组执行相同的操作
所以基本的调用结构应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
把你的实现基本上是
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, 0, pivot) // <<<<<< notice the '0' instead of 'i'
DoSort (array, pivot + 1, j)
}
void DoSort(数组,i,j)
{
pivot=分区(数组,i,j)
DoSort(array,0,pivot)/您没有使用正确的pivot元素。您计算左值和右值的平均值,但必须从子数组中获取一个样本值以进行分区
你可以选择最右边的,中间的或者任何其他的元素,所以你的第一行代码应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
您还可以选择任何其他元素(例如随机)
编辑:这并不能解决性能问题
据我所知,快速排序将把一个数组分成两个子数组A和B,其中A中的所有元素都小于B中的任何元素,然后对这两个子数组执行相同的操作
所以基本的调用结构应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
把你的实现基本上是
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, 0, pivot) // <<<<<< notice the '0' instead of 'i'
DoSort (array, pivot + 1, j)
}
void DoSort(数组,i,j)
{
pivot=分区(数组,i,j)
DoSort(array,0,pivot)/您没有使用正确的pivot元素。您计算左值和右值的平均值,但必须从子数组中获取一个样本值以进行分区
你可以选择最右边的,中间的或者任何其他的元素,所以你的第一行代码应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
您还可以选择任何其他元素(例如随机)
编辑:这并不能解决性能问题
据我所知,快速排序将把一个数组分成两个子数组A和B,其中A中的所有元素都小于B中的任何元素,然后对这两个子数组执行相同的操作
所以基本的调用结构应该是这样的
partitionSortRecursive(array, 0, lastS1);
partitionSortRecursive(array, firstS3, end);
int pivot = array[(beginning + end) / 2];
// or
int pivot = array[end];
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, i,pivot)
DoSort (array, pivot + 1, j)
}
把你的实现基本上是
void DoSort (array, i, j)
{
pivot = Partition (array, i, j)
DoSort (array, 0, pivot) // <<<<<< notice the '0' instead of 'i'
DoSort (array, pivot + 1, j)
}
void DoSort(数组,i,j)
{
pivot=分区(数组,i,j)
DoSort(array,0,pivot)//您可以在代码审阅时发布此消息。什么是代码审阅