[英]Hybrid QuickSort + Insertion sort java.lang.StackOverflowError
[英]Quicksort with insertion sort in java
您好,我一直在嘗試根據老師告訴我們的方法,使用插入排序來實現我的quicksort算法,但我只是無法弄清楚該怎么做...而且我老師的偽代碼似乎對我沒有多大幫助...無論如何,我在網上搜索過,但找不到任何東西。 這是他給我們的偽代碼
voidTQuickSort(int[]A, low,high)
while low < high do
p = DPartition(A,low,high)
TQuicksort(A,low,high)
low = p + 1
end while
他還說:“在快速排序切換到insertSort以便對小子數組進行遞歸調用后,進行分區,首先遞歸較小的子數組,然后對較大的子數組進行尾部調用”
首先,我想知道什么是尾叫,他在說哪個數組? 在我最初的快速排序中,我有這個
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循環中,我的運行時間將從quicksort上的1 ms變為所謂的“改進的” quicksort的24 ms
這是嘗試
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);
}
任何幫助表示贊賞! 謝謝
函數分區和插入排序2
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]);
}
}
}
首先,您應該編寫一個測試來檢查您的方法是否對數組進行了實際排序。 這樣可以節省您很多時間。
他還說:“在快速排序切換到insertSort以便對小子數組進行遞歸調用后,進行分區,首先遞歸較小的子數組,然后對較大的子數組進行尾部調用”
首先,我想知道什么是尾叫,他在說哪個數組?
尾部調用是方法中的最終調用。
我認為他希望您在分區之后首先對兩個子數組中最短的然后對最長的進行排序。 我不確定他為什么要這么做。
他還要求將插入排序用於小型子陣列。 因此,您的快速排序應如下所示:
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.