在下面列出的代码中,我试图找出如何将其转换为迭代解决方案。我理解递归的概念,但我正在尝试开发一个快速排序程序,并且还希望显示迭代过程。
public void recursiveSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number, I am taking pivot as middle index number
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex < j){
recursiveSort(lowerIndex, j);
}
if (i < higherIndex){
recursiveSort(i, higherIndex);
}
}
我想修改的部分是....
// call recursiveSort() method recursively
if (lowerIndex < j){
recursiveSort(lowerIndex, j);
}
if (i < higherIndex){
recursiveSort(i, higherIndex);
}
感谢您提供的任何帮助!!