Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sorting Arrays
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shiro786




PostPosted: Thu Sep 13, 2007 8:02 pm   Post subject: Sorting Arrays

Hey everyone,

So basically, I'm trying to figure out a way to automatically sort my integer array in order from greatest to least, or least to greatest. What would be the simplest way to accomplish this?

Code is located here: http://pastebin.ca/696682 if you would like to better understand what I'm trying to do.

Essentially, I'm trying to sort out the student array so that the data is outputted by descending or ascending order of the aforementioned array.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Sep 13, 2007 8:34 pm   Post subject: RE:Sorting Arrays

The simplest way would be Bubble Sort, but there are many other sorting algorithms as well.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Euphoracle




PostPosted: Thu Sep 13, 2007 10:06 pm   Post subject: RE:Sorting Arrays

I rewrote the code in Java, in case you never learned Turing, hope it helps Smile

http://pastebin.ca/696831
wtd




PostPosted: Fri Sep 14, 2007 12:45 am   Post subject: RE:Sorting Arrays

Would it be wrong of me to suggest Arrays.sort? Smile
Dan




PostPosted: Fri Sep 14, 2007 2:17 am   Post subject: Re: RE:Sorting Arrays

wtd @ 14th September 2007, 12:45 am wrote:
Would it be wrong of me to suggest Arrays.sort? Smile


I whould recomend learning how to sort befor using the built in sort methods in java so you understand them. (Same thing with the search ones).

After you understand it tho, you might as well save time and use the bulit in ones unless you have a reason not to. Less coding, less to debug and less to go wrong.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Euphoracle




PostPosted: Fri Sep 14, 2007 6:21 am   Post subject: RE:Sorting Arrays

The Sort method that Java has is far more complex than Bubble sort Smile. My recommendation would be to follow through bubble sort and understand how it works. Sun opensourced the Java libraries, so then if you really want to, you can pop into src.zip in your JDK folder, and browse to it. You can find the class & file by searching for it on the sun website. Javadocs and whatnot.
Dan




PostPosted: Fri Sep 14, 2007 10:26 am   Post subject: Re: Sorting Arrays

It is a quick sort, well a slightly tweaked one.

As sun puts it:

sun wrote:

The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.


It is not that hard to understand quick sort, the tweaked one maybe be a bit more complicated (i have not looked) but that dose not mean you should not try to learn the basicks.

Also it is imporent to have some idea of what it is doing epstaly when you come to searching or you will be whondering why Arrays.binarySearch is not working on your unsorted array.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Shiro786




PostPosted: Mon Sep 17, 2007 9:51 pm   Post subject: RE:Sorting Arrays

Sorry I didn't reply fast enough. But yeah, I've tried using Arrays.sort, but I don't know what the problem is. It won't let me use it directly. Using my code as reference, is it possible to tell me how to insert the neccessary method in order to call the function Array.sort?

Thanks in advance,


Shiro786



Edit; Alright, I know what was wrong and I fixed my problem. But now that I know how to use Arrays.sort I was going on a hunch that arrays inputted (code here: http://rafb.net/p/ozoua470.html) were linked in order of input. Big mistake, and now I'm stuck. Any other ideas to help me out? Or is this technically a new topic deserving question? >_>
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: