3 Replies - 4876 Views - Last Post: 02 February 2011 - 05:45 PM Rate Topic: -----

#1 Pharmicyst  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 11

Sorting an two arrays together

Posted 02 February 2011 - 04:15 PM

I'm having trouble sorting two arrays.

My assignment is this:
"Write a GradeSort class according to the attached specification. The sort method in this class will sort an array of grades that is passed to it in descending order. The array of names should be re-ordered according to the re-ordering of the grades array.

HINT: The sort method can use the code from either InsertionSort or SelectionSort to sort the grades array. Whenever, the sort routine exchanges elements of the grades array, it should also exchange the corresponding elements of the names array,

Test your GradeSort class with the attached test program.

Once you have the GradeSort class working properly, write your own main program that reads at least 4 grades and 4 names from the user. It should then sort these values, using the GradeSort class and then print the names and grades. "

I wasn't familar with InsertionSort or SelectionSort, so I tried to do a bubble sort and sort the arrays at the same time, however whenever our test program throws this at it:
int[] scores5 = { 45, 17, 63, 39, 71 };
			String[] names5 = { "Dave", "Alan", "Glenda", "Betty", "Robert" };

It doesn't match this:
			int[] sorted5 = { 71, 63, 45, 39, 17 };
			String[] sortedNames5 = { "Robert", "Glenda", "Dave", "Betty",
					"Alan" };



My code:
import java.lang.reflect.Array;
import java.util.*;



public class GradeSort {

	private static String[] names;    
	private static int[ ]grade;
	
    
    	
	public void sort(String[] names, int[] grade){
		if (names.length == 0)
			throw new IllegalArgumentException();
		if (grade.length == 0)
			throw new IllegalArgumentException();
		if (names.length != grade.length)
			throw new IllegalArgumentException ("Must be same size");

		for(int j=0;  j < grade.length -1;  j++ )
        {
               if ( grade[ j ] < grade[j+1] )   
               {
                       int temp;
                       String tempname;
                       temp = grade[j];
                       tempname = names[j];
                       grade[j] = grade[j+1];
                       names[j] = names[j+1];
                       grade[j+1] = temp;
                       names[j+1] = tempname;
                     
               }
        } 
	}
}


I'm new here and just looking for some help! Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting an two arrays together

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Sorting an two arrays together

Posted 02 February 2011 - 04:26 PM

Bubble sort should have 2 for loops.
Search the site for bubble sort, insertion sort (and any other sort method actually) you'll find plenty of tutorials and code snippets here :)

here is the first snippet I found:
http://www.dreaminco.../snippet513.htm
feel free to search for more
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Sorting an two arrays together

Posted 02 February 2011 - 04:46 PM

Here's another example.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Sorting an two arrays together

Posted 02 February 2011 - 05:45 PM

Parallel arrays aren't good practice. Design a Student class instead and store a Student[]. For more information on class design, check out my tutorial Moving Away From Parallel Arrays.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1