根据我拥有的材料,我正在学习java中的快速排序。最好的情况是枢轴是中位数,最坏的情况是枢轴的一侧总是空的。
对于以下代码,“indexPartition”是枢轴吗?您在以下函数中放置了什么样的参数,以便在最坏的情况下运行?
private void quickSortSegment(E[] list, int start, int end)
{
if (end-start>1)
{
int indexPartition = partition(list, start, end);
quickSortSegment(list, start, indexPartition);
quickSortSegment(list, indexPartition+1, end);
}
}
private int partition(E[] list, int start, int end)
{
E temp;
E partitionElement = list[start];
int leftIndex = start;
int rightIndex = end-1;
while (leftIndex<rightIndex)
{
while (list[leftIndex].compareTo(partitionElement) <= 0 && leftIndex<rightIndex)
{
leftIndex++;
}
while (list[rightIndex].compareTo(partitionElement) > 0)
{
rightIndex--;
}
if (leftIndex<rightIndex)
{
temp = list[leftIndex];
list[leftIndex] = list[rightIndex];
list[rightIndex] = temp;
}
}
list[start] = list[rightIndex];
list[rightIndex] = partitionElement;
return rightIndex;
}