• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

sorting an array of integers - a problem.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:[email protected]
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you like fries with that?
 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sounds like a homework assignment for a java class in school...
 
guru mysore
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Greg Harris:
sounds like a homework assignment for a java class in school...



Thanks anyway!

 
guru mysore
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Greg Harris:
sounds like a homework assignment for a java class in school...


Thanks for your patience..
Guru
 
guru mysore
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Daniel Dunleavy:
Would you like fries with that?


Thanks anyway!
Guru
 
guru mysore
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guru,
Once you have written the code then post it here so that we can use it.
cheers !
Gaurav
------------------
http://www.mantrotech.com
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 );
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:[email protected]


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
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic