Hi all, i have this mini program that generates randomly 15 to 25 sized array.
This array then randomly generate lower and upper case letters.
So far i have gotten iterative swap to move all lower case to left side and all upper case to right side. Im' required to do it recursively too. but am having a hard time figuring out the base case and general case. here is the code i have for the parts i got working.
Minor point: C++ already has an std::swap() function in the algorithm header.
(Where you'd also find a ready-to-use std::sort() if you weren't graded for writing your own).
#include <algorithm>
void sortArray(char myArray[], int left, int right)
{
if (left >= right)
return;
swap(myArray[left], myArray[(left + right) / 2]);
int last = left;
for (int i = left+1; i < right; ++i)
if (myArray[i] > myArray[left]) // this condition can be refined for "nicer" sorting
swap(myArray[i], myArray[++last]);
swap(myArray[left], myArray[last]);
sortArray(myArray, left, last-1);
sortArray(myArray, last+1, right);
}
With recursive algorithms, you'd generally try to break your input into ever smaller pieces, so your exit condition will usually depend on that.