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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdlib>
#include <ctime>
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).

1
2
3
#include <algorithm>
//...
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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.
Topic archived. No new replies allowed.