### Recursive function for sorting in array

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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include #include using namespace std; const int MAXSIZE = 25; void ConstructArray (char [], int); void printArray (const char [], int); void iterativeSwap (char [], int); int main () { char myArray [MAXSIZE]; srand (time(NULL)); int randomSize = rand() % 11 + 15; ConstructArray (myArray, randomSize); //printArray (myArray, randomSize); //iterativeSwap (myArray, randomSize); } void ConstructArray (char myArray [], int randSize) { char randUppcase, randLowcase; int randAlpha; for (int i = 0; i < randSize; i++) { randAlpha = rand() % 2 + 1; randUppcase = rand() % 26 + 65; randLowcase = rand() % 26 + 97; if (randAlpha == 1) randAlpha = randUppcase; else randAlpha = randLowcase; myArray [i] = randAlpha; } printArray (myArray, randSize); iterativeSwap (myArray, randSize); } void printArray (const char myArray [], int randSize) { cout << "Given the following array " << endl; for (int i = 0; i < randSize; i++) cout << myArray[i] << " "; cout << endl << endl; } void iterativeSwap (char myArray [], int randSize) { int i = 0; int j = randSize - 1; char temp; while (i != j) { if (myArray[i] >= 'A' && myArray[i] <= 'Z') { if (myArray[j] >= 'a' && myArray[j] <= 'z') { temp = myArray[i]; myArray[i] = myArray[j]; myArray[j] = temp; } else --j; } else ++i; } cout << "Iterative Swap" << endl; for(int i = 0; i < randSize; i++) cout << myArray[i] << " "; }``````
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).

 ``123`` ``````#include //... swap(myArray[i], myArray[j]);``````

Anyway. There are more sorting algorithms available.
http://en.wikipedia.org/wiki/Sorting_algorithm

This below is QuickSort, kind of.

 ``12345678910111213141516171819`` ``````#include 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.
Topic archived. No new replies allowed.