您好我一直在尝试使用插入排序来实现我的快速排序算法,这取决于我们的老师告诉我们的方式,但我无法确切地知道该做什么......以及我老师的伪代码似乎对我帮助不大......无论如何,我在网上搜索但我找不到任何东西。 这是他给我们的伪代码
voidTQuickSort(int[]A, low,high)
while low < high do
p = DPartition(A,low,high)
TQuicksort(A,low,high)
low = p + 1
end while
他还说&#34;在快速排序中切换到插入以便对小子阵列进行递归调用,在分区之后,首先递归较小的子阵列然后对较大的子阵列进行尾调用#34;
首先,我想知道,什么是尾调用,他在谈论哪个阵列?在我原来的快速排序中,我有这个
public void quicksort(int numbers[], int start, int end)
{
//Condition to continue recursion
if(start < end)
{
//Set up my index as the start or end point for recursion
int myIndex = partition(numbers, start, end);
//Recursive algorithm for left and right side of the array
quicksort(numbers, start, myIndex-1);
quicksort(numbers, myIndex + 1, end);
}
}
在我的尝试中,我得到了堆栈溢出,但是,如果我将这两个子数组放回while循环中,我的运行时间从快速排序的1 ms到所谓的#34;改进的#24的24 ms 34;快速排序
这是尝试
public void quicksortImproved(int numbers[], int start, int end)
{
//Condition to continue recursion
while(start < end)
{
//Set up my index as the start or end point for recursion
int myIndex = partition(numbers, start, end);
//Recursive algorithm
quicksortImproved(numbers, start, end);
start = myIndex + 1;
}
insertionSortTwo(numbers);
}
任何帮助表示赞赏!谢谢
功能分区和insertionsortTwo
public int partition(int[] numbers, int start, int end)
{
//Declaring my pivot, start and end indexes
int pivot = numbers[end];
int small = start;
int big = end - 1;
//Begin loop while index of small is less than index of big
while(small <= big)
{
//If the element @ numbers[small] is smaller than element at pivot
//Then increment small index by 1
while (small <= big && numbers[small] <= pivot)
{
small++;
}
//If the element @ numbers[big] is bigger than element at pivot
//Then decrement index by 1
while(big >= small && numbers[big] >= pivot)
{
big--;
}
//If small index is smaller than big index
//Swap elements @ number[small] with element @ number[big]
if(small < big)
{
numbers[small] = returnFirst(numbers[big], numbers[big] = numbers[small]);
}
}
//Swap pivot with the presumed middle element and return small index
numbers[end] = returnFirst(numbers[small], numbers[small] = numbers[end]);
return small;
}
public void insertionSortTwo(int[] numbers)
{
//Loop from index 1 to end, element @ numbers[0] is in the sorted array
for (int i = 1; i < numbers.length; i++)
{
//Setting up second array index variable
//Loop to create new array in sorted order
for(int j = i; j > 0 && numbers[j] < numbers[j - 1]; j--)
{
//If element @ number[j] is smaller than element @ number[j - 1]
//swap element @ numbers[j] with element @ numbers[j - 1] and decrement j
numbers[j] = returnFirst(numbers[j - 1], numbers[j - 1] = numbers[j]);
}
}
}
答案 0 :(得分:0)
首先,您应该编写一个测试检查您的方法是否实际排序了数组。它会为你节省很多时间。
他还说“在快速排序中切换到插入方式,用于对小子阵列进行递归调用,在分区之后,首先递归较小的子阵列,然后对较大的子阵列进行尾调用” 首先,我想知道,什么是尾调用,他在谈论哪个阵列?
尾调用是方法中的最后一次调用。
我认为他希望你在分区后首先对两个子阵列中的最短子阵列进行排序,然后进行最长的排序。我不知道为什么他会这么想。
他还要求对小型子阵列使用insertionsort。因此,你的快速排序应该是这样的:
public void quicksort(int numbers[], int start, int end) {
if(start < end) {
if(start + 10 > end) {
insertionsort(numbers, start, end);
} else {
int myIndex = partition(numbers, start, end);
quicksort(numbers, start, myIndex-1);
quicksort(numbers, myIndex + 1, end);
}
}
}