In the code listed below I am trying to figure out how to turn it into a iterative solution. I understand the concept of recursion but I am trying to develop a quick-sort program and also want to show the iterative process.
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);
}
}
The part that I am trying to modify is....
// call recursiveSort() method recursively
if (lowerIndex < j){
recursiveSort(lowerIndex, j);
}
if (i < higherIndex){
recursiveSort(i, higherIndex);
}
Thanks for any help offered!!