• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting an array using loops  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing something wrong here..  I am trying to sort these numbers in the array using loops.  I am trying to compare sub[0] in a to sub[1] in b, then 0 and 2, 0 and 3.. then 1 and 2, 1 and 3,  etc etc.. to where it has them all compared , and then sorts them .
I do not want to use an arraySort command , just loops.  I put the System.out ...... in different places.. trying to see the value of N, and value of c, but i do not get numbers in order, matter of a fact one of them lists numbers that are not even in my array.. I got something messed up.   Any help to get me on the right track understanding arrays more .. Thank you.,

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Dagenhart wrote:I am doing something wrong here..  I am trying to sort these numbers in the array using loops.  I am trying to compare sub[0] in a to sub[1] in b, then 0 and 2, 0 and 3.. then 1 and 2, 1 and 3,  etc etc.. to where it has them all compared , and then sorts them .
I do not want to use an arraySort command , just loops.  I put the System.out ...... in different places.. trying to see the value of N, and value of c, but i do not get numbers in order, matter of a fact one of them lists numbers that are not even in my array.. I got something messed up.   Any help to get me on the right track understanding arrays more .. Thank you.,



Hm.... I think you should try using a loop and print out every value of N. 
 
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:

Is this not the result that you're expecting? If not, what were you expecting?
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh that is the result.. I could have sworn I looked at the value of N and it didnt give me what you all had.. I wrote the code correct then?
minus getting it to print out correctly
 
Master Rancher
Posts: 2046
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the code is correct, if the intention was to sort from high to low. What if you want to sort from low to high? Can you make the code such that this is an option?
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe so.. it would require changing if( N[a] < N[b] ) sign to >
just running that change by itself makes them run low to high. I could also reverse the order that it reads the numbers starting a and B at the end of their index, and using a-- b--  to run through the checking from the end to front.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!