This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes sorting an array of integers - a problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sorting an array of integers - a problem." Watch "sorting an array of integers - a problem." New topic
Author

sorting an array of integers - a problem.

guru mysore
Greenhorn

Joined: May 23, 2001
Posts: 9
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
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
Would you like fries with that?
Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
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!
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
sounds like a homework assignment for a java class in school...


what?
guru mysore
Greenhorn

Joined: May 23, 2001
Posts: 9
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
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
Originally posted by Daniel Dunleavy:
Would you like fries with that?

Thanks anyway!
Guru
guru mysore
Greenhorn

Joined: May 23, 2001
Posts: 9
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!

Thanks for your patience and encouragement!
Guru
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Guru,
Once you have written the code then post it here so that we can use it.
cheers !
Gaurav
------------------
http://www.mantrotech.com


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
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 Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

and besides... using Array objects is for wimps.

us C/C++ folk (non STL!) had to code bubble sorts and binary sorts... this is excellent mind work and good coding practice.

Just kidding about the first sentence, but the second one, not at all.
Ender Everett
Greenhorn

Joined: Mar 31, 2001
Posts: 19
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.
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
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
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi Ender,

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).]
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
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.
Kirill NKaufmann
Ranch Hand

Joined: May 25, 2001
Posts: 46
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.


thats a good policy

Scott Hiett
Greenhorn

Joined: Jun 05, 2001
Posts: 5
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
Rick Rodriguez
Ranch Hand

Joined: Jun 02, 2001
Posts: 44
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.
J. Moritz
Greenhorn

Joined: Nov 18, 2004
Posts: 9
Hi again,

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).

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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

I like this solution. It is very elegant, imho.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sorting an array of integers - a problem.
 
Similar Threads
How to cast arrays?
Simple array doubt?
is it a Object?
Passing a collection in request
how to modify this program to get asending order integers?