Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise
void sortRooms()

I came up with this algorithm to sort my room struct. Inside room struct there are only 3 values (50, 120, 30) which is why I have to decrease the index by at the start and increase dec by 1 everytime. However, when i compile and it prints out

OHE 100 120
SAL 210 30
OHE 120 50

I traced out the steps on paper but I cant seem the follow why the index is not incrementing correctly so that it would go on to compare 120 with 50 and then proceed to switch those values. Any help would be appreciated.

share|improve this question
1  
The whole thing should look like: std::sort(room.begin(), room.end(), MaxStudentsCompare). – Don Reba Mar 11 '14 at 6:48
up vote 0 down vote accepted

For i=0, you find the largest element (of all elements, except the last one, since dec starts off at 2, not 1) and put it in the 0th position.

Then, for i=1, you find the largest element (again of all elements, except the last one, which includes the first) and put it in the 1st position.

And so on.

You can't sort like that - sure, the largest element will end up at the end (which, based on your comment to the other answer, isn't what you want), but there's nothing ensuring that any of the other elements are actually sorted.

If you want to have it sorted from largest to smallest, in your inner loop, you need to stop when you get to i, so you're only picking the largest element of the remaining elements.

This is very similar to selection sort, so you could look at that for further information, or optimizations (like keeping track of the maximum and just swapping at the end, rather than swapping at each bigger element we find).

share|improve this answer
    
yup got it to work with a simple else statement – user3255966 Mar 11 '14 at 6:59

Why don't you change the while loop into a for loop? For example:

for (int dec=2; (jk[0]-dec+1) > 1; dec++)
{
    if (room[i].max_students > room[jk[0] - dec].max_students)
    {            
        TEMPbuilding_code = room[i].building_code;
        room[i].building_code = room[jk[0] - dec].building_code;
        room[jk[0] - dec].building_code = TEMPbuilding_code;

        TEMProom_number = room[i].room_number;
        room[i].room_number = room[jk[0] - dec].room_number;
        room[jk[0] - dec].room_number = TEMProom_number;

        TEMPmax_students = room[i].max_students;
        room[i].max_students = room[jk[0] - dec].max_students;
        room[jk[0] - dec].max_students = TEMPmax_students;
    }
}

Also, did your order of operations in the while loop mess up? I suppose it should be jk[0] + 1 - dec or jk[0] - dec + 1?

share|improve this answer
    
editted my code with a new for loop, I actually want to try and place them in decreasing order (highest first, lowest last). It still outputs the wrong values though – user3255966 Mar 11 '14 at 6:46
    
@hofmeister Note that > in code doesn't get converted to > - it remains as is, so you changing > to > in the code isn't a good edit, not that I'm sure why you'd do that in the first place. – Dukeling Mar 11 '14 at 6:54
    
@Dukeling Oh sorry. Probably an encoding error. Copied the code after editing the format. – hofmeister Mar 11 '14 at 7:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.