Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting multidimensional arrays (int)

 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there.
i am trying to create a simulator for RoundRobin Process Scheduling Method.

i have this piece of code..



and the output as


P1 on index 0 = 20
P1 on index 1 = 20
P1 on index 2 = 20
P1 on index 3 = 20
P1 on index 4 = 10
P2 on index 0 = 20
P2 on index 1 = 20
P2 on index 2 = 20
P2 on index 3 = 20
P2 on index 4 = 20
P2 on index 5 = 20
P2 on index 6 = 10
P3 on index 0 = 20
P3 on index 1 = 10
P4 on index 0 = 20
P4 on index 1 = 20
P4 on index 2 = 20
P4 on index 3 = 10
P5 on index 0 = 20
P5 on index 1 = 20
P5 on index 2 = 10

Total = 21


I've realized that the output that I need is the sorted index...
it would probably mean that i need to sort the [j] index here.

or i need to do something else.
any idea?

i need to come up with like this one


P1 on index 0 = 20
P2 on index 0 = 20
P3 on index 0 = 20
P4 on index 0 = 20
P5 on index 0 = 20
P1 on index 1 = 20
P2 on index 1 = 20
P3 ....


and so on...

please help...
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[pedantic mode]There is no such thing as a multi-dimensional array in Java, only arrays of arrays; you will see the Java™ Language Specification says the component of an array may itself be an array.[/pedantic mode]

You are trying to do it the hard way. You need to learn how to sort an int[] using the < or > operators. Hint: start by making a swapPairInArray() method like this.Overload your method to take an Object[].
Look up sorting algorithms, for example in Niklaus Wirth's book, available free on the net. Implement one of those algorithms for int[] arrays, then for Comparable[] arrays. As an example, try a String[] because String implements Comparable.Then work out how to sort things when their members are already arrays. You will have to work out what criterion you are using to tell whether {1, 2, 3) is greater or less than {3, 2, 1}.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic