Computer Science Canada

Sorting algorithm

Author:  Mikez [ Fri Dec 13, 2002 12:32 pm ]
Post subject:  Sorting algorithm

plz tell or show me an advanced form of Sorting algorithms (not bubble) for me to use. include code if possible.
ps thanks in advance Laughing

Author:  FizixMan [ Fri Dec 13, 2002 1:09 pm ]
Post subject: 

Helps if we knew what we were sorting, and how they are to be sorted.

Author:  Mikez [ Fri Dec 13, 2002 7:39 pm ]
Post subject:  Sorting

I will be sorting the scores of players in a game
mike's score is 100
adam's score is 11

they need to be sorted from highest to lowest or vice verse

Author:  Mikez [ Fri Dec 13, 2002 7:42 pm ]
Post subject: 

also the more advanced the better but if u can please give a couple.
(if you have the time)

thanx u guys are a HUGE help. keep up the good work

Author:  FizixMan [ Fri Dec 13, 2002 10:55 pm ]
Post subject: 

Well... I'm assuming you'll be inputing a number of scores.. I'm also assuming you know how man scores to be inputed. Now, I could write code for you to pretty much make a dynamic sized array... but that would probably be more than your teacher or yourself are really capable of (so I'm not going to make it that advanced for you) in fact, I'm going to help out with the basics, you can get that working... and if you have any ideas on how to make it more advanced, well ask and we'll see if we can help you...

but this deal with people just asking for the complete source code to class homework and projects is pretty lame... and believe me... all offense intended Razz j/k Again... keep asking for the code if you want, I don't intend to just write it for you (even though I always do write the program to make sure what I'm giving you works... so easy to just copy paste the code... but it's so much better that you all learn how to code it from scratch)

okay, well... if you know how many scores are being put in, just declare an array of integers with that many elements. Then you'll want a "for loop" to go through the scores and sort them... also, I'm assuming you'll just want these outputted to the screen and not restored in an array... if you want them stored in an array, just change the part where it prints to the screen with a line that saves it to an array element.


declare array of integers with size N
declare variable X as integer with value 0  %to be used to sort
declare variable Y as integer %to be used to remember which scores are used

user inputs scores to each element

for c:1..N

for count:1..N
      is the array at index N greater than X?
             true: assign X the value of the array element
                     assign Y the value of 'count' %which score is the highest
             false, restart the loop to check the next array element
end for

%now at this point, you've gone through the array, and X should be your largest score, and Y should be the element number that the highest score was assigned to in the array

output X
set the array element Y to 0 %that way when it rechecks the array, it won't re-choose that biggest element again since when it checks to see if X is greater than it, it will read 0

Now it restarts the whole process over again... this will happen N times
end for

I hope you understand that