function to split and rearrange array

Hi, this is actually for a homework project, so I'm looking for some guidance. I need to write a function that will take in an array and split the function, rearranging the array by putting any elements < splitter before the splitter string and elements > splitter after it. The arguments must remain the way they are. The function works for the most part, except when the splitter string appears more than once in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string f[6] = { "grant", "buster", "kari", "tory", "adam", "jamie" };
int r = split(f, 6, "jessi");  //  returns 4
	// f might now be
	//	"grant"  "buster"  "jamie"  "adam"  "tory"  "kari"
	// or   "jamie"  "adam"  "buster"  "grant"  "kari"  "tory"
	// or one of several other orderings.
	// The first 4 elements are < "jessi"; the last 2 aren't.
string g[4] = { "jamie", "adam", "buster", "narrator" };
int s = split(g, 4, "buster");  //  returns 1
	// g must now be either
	//	"adam"  "buster"  "narrator"  "jamie"
	// or	"adam"  "buster"  "jamie"  "narrator"
	// All elements < "buster" (i.e., "adam") come before all others.
	// All elements > "buster" (i.e., "jamie" and "narrator") come
	//      after all others. 


This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int split(string a[], int n, string splitter)
{
	if(n < 0)
		return -1;
	int i = 0;
	int j = n-1;
	string swap;
	while(i < j)
	{
		if(a[i] >= splitter)
		{
			swap = a[j];
			a[j] = a[i];
			a[i] = swap;
		}
		if(a[i] < splitter)
			i++;
		if(a[j] > splitter)
			j--;
	}
	if(j == n-1)
		return n;
	return i;
}
Topic archived. No new replies allowed.