Sorting Parallel Arrays

Pages: 12
Hey guys,
this is my first time working with parallel arrays, and while I think I have a good understanding of them and how to sort them using digits, I have yet to understand or find a good example online on sorting using strings. Can anyone explain how to sort parallel arrays in descending order by string?
I can't think of an example of where parallel arrays are any better than structures. Here's how I'd do it if I had an array of students with names and student numbers.

Example:
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
struct student
{
  string name;
  int student_number;
  bool operator< (student i, student j) { return (i.name<j.name); }
} students[10];

// populate the array. Then do this to sort it:

bool sorted = false;
while (!sorted)
{
    sorted = true;
    for (int i = 0; i < 9; ++i)
    {
        if (students[i] < students[i+1])
        {
            student temp = students[i];
            students[i] = students[i+1];
            students[i+1] = temp;
            sorted = false;
        }
    }
}
Last edited on
It's is for school, and I am required to use parallel arrays.
Trust me, I have heard from many people that there are many ways to do it differently...and better...as in more efficient :/ but... requirements are requirements.. and the examples my professor gives us...do not sort by string...but by int...and he wants us to sort by string
I don't even think I've seen the words parallel arrays since my intro to C++ class lol. Anyway, strings are nothing more than a series of characters, which are nothing more than ints themselves. In memory, everything is the same. Look at this: http://www.asciitable.com/
As you can see, as you go up in alphabet the value of each letter also increases.

A handy function may be this: http://www.cplusplus.com/reference/string/string/compare/

Does this help?
Yeah sort of...i guess. I mean i understand where you are coming from, just not how to put it into code. I got this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void SelectionSort (string Customer[], int counter)
{
	int i;
	int j;
	int min;

	for (i = 0; i < counter - 1; i++)
	{
		min = i;

		for (j = i + 1; j < counter; j++)
			if (Customer[j] < Customer[min])
			min = j;

		Customer[i].swap(Customer[min]);
	}
}
OMG IT WORKS!!!!!! :) Thanks for your help!
Looks like it'll sort that array, but how do you plan on keeping the other array lined up with that?
Hmm...that is a good question. Let's see if i can come up with something.
*Hint, wherever you swap your Customer[] array, swap your parallel array as well. It'll be only 1 extra line.
Last edited on
??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void SelectionSort (string Customer [], double Charge [], int counter)
{
	int i;
	int j;
	int min;

	for (i = 0; i < counter - 1; i++)
	{
		min = i;

		for (j = i + 1; j < counter; j++)
			if (Customer[j] < Customer[min])
			min = j;

		Customer[i].swap(Customer[min]);
		Charge[i].swap(Charge[min]);
	}
}
No, that doesn't work. I obviously did something wrong lol
That's going to swap on each iteration. Maybe you forgot your braces for the if? :)
I guess I don't understand what I am going to swap.
I don't want to do what I have there, I need them to be in descending order according to Customer, not Charge. So if i do what I have done above, it will put both in descending order. I need to Charge array to line up with the Customer array once that has been sorted.
How can swap do that?
Think about like this:

if(customerArr need swapped)
Swap customerArr AND chargeArr

If you just make sure to swap them both at the same time, they'll stay matched up.
closed account (o3hC5Di1)
I think what Mr. ResidentBiscuit was referring to was this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void SelectionSort (string Customer [], double Charge [], int counter)
{
	int i;
	int j;
	int min;

	for (i = 0; i < counter - 1; i++)
	{
		min = i;

		for (j = i + 1; j < counter; j++) // braces here:
                {
			if (Customer[j] < Customer[min]) 
                        {
			    min = j;
                        }
                }

		Customer[i].swap(Customer[min]);
		Charge[i].swap(Charge[min]);
	}
}


Once you only sort the Customer array and not the Charge array, they won't be parallel any more and there will be no more way to link one to another (without overcomplicating this issue any further).

Hope that helps.

All the best,
NwN
Last edited on
I guess the reason it wasn't working before is...
I keep getting an error that says expression must have class type (referring to the first Charge on line 18.
@NwN,

Actually, not quite. Those braces do the same as not having them at all. But, I didn't know the selection sort, had to look it up.

1
2
3
if ( iMin != j ) {
        swap(a[j], a[iMin]);
    }


Looks like you're missing this line.

http://en.wikipedia.org/wiki/Selection_sort
:/ so confused...
closed account (o3hC5Di1)
Apologies, just thought I'd quickly help the OP on this - should have read it more carefully, my bad!

All the best,
NwN
Well, what's going wrong currently?
Pages: 12