0

I am asked in an assignment in java course to sort multiple arrays according to the Average of the student

    201302972, AhmadJouni, 82 , 85 , 89 , Average: 85.333336
    201303123, Joe, 70 , 75 , 96 , Average: 80.333336
    201401034, Mohamad, 90 , 54 , 49 , Average: 64.333336
    201402100, Ali, 80 , 90 , 99 , Average: 89.666664

My arrays :

int ID[] = new int [4];
int Grades[][]= new int [4][3];
String Name[] = new String[4];

I have to sort all of the above according to averages in increasing order

Final output should look like

        201401034, John, 90 , 54 , 49 , Average: 64.333336
        201303123, Joe, 70 , 75 , 96 , Average: 80.333336
        201302972, Mike, 82 , 85 , 89 , Average: 85.333336
        201402100, Amy, 80 , 90 , 99 , Average: 89.666664

Note : The use of objects/constructors is not allowed , should be done in some simple way. This is the challenge

Full code : http://textuploader.com/1he1

5
  • I Tried to make a string that contains all the line , but didn't succeed in that Commented Feb 19, 2014 at 19:39
  • 2
    A String is an object in Java ... I really think these teachers that don't teach idiomatic Java or Object Oriented analysis and design are doing a dis-service to the students if they are going to use Java. If you want to teach the fundamentals, use C, don't use an OO language and just confuse the students even more! The correct way in Java of course is to use a Comparator and Collections.sort()! These raw array based exercises are not what employers are looking for when you get up at a whiteboard during your first interviews! Commented Feb 19, 2014 at 19:46
  • Yes this is 100% true , teaching programming languages at universities should have a more organized and more efficient approach. I noticed that studying from well known books out there is a lot better and easier to understand Commented Feb 19, 2014 at 19:55
  • 1
    Does that mean you are also not allowed to use Arrays? Arrays are also objects... Commented Feb 19, 2014 at 19:56
  • I mean self created objects like Student object and not objects of Java library Commented Feb 19, 2014 at 20:02

2 Answers 2

1

You can do it the same way as you would do it with a single field, but instead of swapping values in one array, you swap values on the same positions in all arrays at once.

Sign up to request clarification or add additional context in comments.

Comments

1

Create one additional array which contains the integer indices of your data arrays. Then sort the indices rather than the data arrays. By adding multiple index arrays, you could even have multiple sort orders based on different criteria without ever moving the original data.

The sort would order the index set using pseudo-code logic along the lines of:

if (average[index[i]] > average[index[j]]) {
    swap(index[i], index[j])
}

It's not clear from the restriction you cited whether you are supposed to write your own sort or can create a Comparator, but the fundamental idea remains the same: that you rearrange the index set rather than the much more expensive operation of moving multiple arrays worth of data synchronously.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.