This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi All, Can you help me how to write a program to sort an array of integers: A[0]=9 A[0]=3 A[0]=7 in the ascending order? I just want the code instead of explanation of how to do it. Regards, Guru Email:gurumysore1@yahoo.com
Guru, here's something to help get you started. Check out the Arrays class in java.util API. It provides a series of sort() methods for sorting arrays. Check it out, it's really not difficult at all. Good luck!
sounds like a homework assignment for a java class in school...
what?
guru mysore
Greenhorn
Joined: May 23, 2001
Posts: 9
posted
0
Originally posted by Greg Harris: sounds like a homework assignment for a java class in school...
Thanks anyway!
guru mysore
Greenhorn
Joined: May 23, 2001
Posts: 9
posted
0
Originally posted by Greg Harris: sounds like a homework assignment for a java class in school...
Thanks for your patience.. Guru
guru mysore
Greenhorn
Joined: May 23, 2001
Posts: 9
posted
0
Originally posted by Daniel Dunleavy: Would you like fries with that?
Thanks anyway! Guru
guru mysore
Greenhorn
Joined: May 23, 2001
Posts: 9
posted
0
Originally posted by Joe McGuire: Guru, here's something to help get you started. Check out the Arrays class in java.util API. It provides a series of sort() methods for sorting arrays. Check it out, it's really not difficult at all. Good luck!
Come'on guys, Let's at least PRETEND that "guru" really wants to learn this stuff and swallow the sarcasm. Of course it would help my attitude if I weren't helping out someone who calls himself guru but . . . . As Joe said, this will do the trick. int[] myIntArray = { 3,5,2,7,1,9,8,6 }; Arrays.sort( myIntArray );
"JavaRanch, where the deer and the Certified play" - David O'Meara
Daniel Dunleavy
Ranch Hand
Joined: Mar 13, 2001
Posts: 276
posted
0
Originally posted by guru mysore: I just want the code instead of explanation of how to do it.
Sorry Cindy, but the quote above just irked me. Its sounded to me like...I don't want to learn it, just want the code Besides, I liked my reply Dan
Mike, Just to beat a dead horse... here's a quick bubble sorting code I made up for absolutely no reason. Well, actually, there is a reason: I'm new to Java and programming in general so I didn't know there were sorting methods in the Java API... duuuh... it's my second week of Java & programming, so give me a break! My Bubble sort (just a code snippet, not a class or anything functional!):
It's probably not as efficient as a more experienced programmer could have made it, and I think it would be wonderfully slow in any really big array, but that's my bubble sort (so far... I think). Any comments?
Don't sweat the petty stuff, just pet the sweaty stuff.
Originally posted by guru mysore: Hi All, Can you help me how to write a program to sort an array of integers: A[0]=9 A[0]=3 A[0]=7 in the ascending order?
Actually, I'm surprised that no one else pointed out that since all three assignments refer to A[0], there would only be one element in this array with the value 7.
I just want the code instead of explanation of how to do it. Regards, Guru Email:gurumysore1@yahoo.com
OK, this irked me a little too. Basically because in the beginning he asks "can you help me" and in the end he means "can you do my work for me?" Cindy, since you gave him the answer can we get back to the sarcasm? ------------------ I'm a soldier in the NetScape Wars... Joel
Wait a minute, I'm trying to think of something clever to say...<p>Joel
Yes, bubble sorts are pretty slow. I've never seen one written like you've done yours though. It took me a while to realize it produces the same 'efficiency' as the ones I always write:Thanks for sharing your code.
[This message has been edited by Mike Curwen (edited June 05, 2001).]
My policy is not to post code for anyone who just wants answers without wanting to learn how to do it for themselves (which fortunately seems to be very rare here at JavaRanch). I consider this a professional advice forum, not some kind of Java code swap meet.
Originally posted by Scott Appleton: My policy is not to post code for anyone who just wants answers without wanting to learn how to do it for themselves (which fortunately seems to be very rare here at JavaRanch). I consider this a professional advice forum, not some kind of Java code swap meet.
Just cuz I am nit picky, I found that using the default sort just goes by alphanumeric while sorting. I need something to take these values from the DB and sort them. But because they were strings, they came back ordered alphanumerically (ie 1,10,11,2,20,3,3000,4...etc). So I wrote a little NumericComparator to pass into the sort method. Whaddya think?
So a call with (in my case) a Vector(vic) of strings is
Originally posted by Scott Appleton: My policy is not to post code for anyone who just wants answers without wanting to learn how to do it for themselves (which fortunately seems to be very rare here at JavaRanch). I consider this a professional advice forum, not some kind of Java code swap meet.
I would even go as far as to say, why would you want the answer, if you do not know how to interpret it? Unless you're taking a Java course just as a college elective and not to utilize it in the real world, as I hope to do one day, I could not see someone utilizing that 'code swapping' mentality. Even so, what a waste of college money, if you do not want to learn the logic behind what you are coding. This is the "harmony/beauty" behind wanting to code in the first place. Ok, I think I'll get off my soap box and discontinue from beating this subject in to the ground. Best of luck Guru. I think Cindy Glass's post probably answered your question.
The sort method of the Arrays class works perfectly, but what I need is a way to find the minimum and maximum values of an array and print these values onscreen with their corresponding index number (non-sorted index numbers).
To find the largest, you could write a loop that looks at each element through the array. at each step, compare it to the 'biggest found so far', which could initially be 0 (something to consider: what if elements can be negative?)
anyway, at each element, see if the value you're looking at is bigger than the one you have. If it is, you need to replace the 'biggest so far', and remember where you are in the array.
to find the smallest would be almost identical, and both could be done in a single pass through the array.
Never ascribe to malice that which can be adequately explained by stupidity.
Originally posted by Scott Hiett: Just cuz I am nit picky, I found that using the default sort just goes by alphanumeric while sorting.
I need something to take these values from the DB and sort them. But because they were strings, they came back ordered alphanumerically (ie 1,10,11,2,20,3,3000,4...etc).
So I wrote a little NumericComparator to pass into the sort method. Whaddya think?
So a call with (in my case) a Vector(vic) of strings is