May 01, 2024, 03:18:04 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


sorting a list of numbers ?

Started by hugh, November 29, 2008, 10:41:09 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

hugh

I have been looking at the sample sort .eba with Ebasic, finding it hard to grasp.

I need to sort numbers from min to max

i have the following.   Rnum=random number ,  ie  rnum=rand 1,number  , is either 75 or 90 number
                              mycount=total of rnum called, ie mycount++
                              out[mycount] = array to store each number as it is called,  ie could be, 66,01,73,21,39, etc

I need to sort the , change[j]  array
I have started with this piece of code, just to swap from 1 array to another
the following is the start of my program in declaring variables i have

int picked[91],out[91],change[91]
for j=1 to number  :                     /* number is either 75 or 90 */
picked[j]=0:out[j]=0:change[j]=0
next j
/*************************************************************************************************************************/
                                        /* this is what i have ben trying to do so as leave the out[j] the same and change[j] to have the value of out[j]
for j=1 to  mycount :           /* mycount assigned within main program  for each random number called , mycount++ */
change[j] = val(out[j]) :      /*need to sort change[j] in numerical order min to max */
next j                           


i just cant remember the formulae for the sort

Regards

Hugh

Barney

What I can't understand is why people run straight to the forum where they can wait some considerable time to get an answer, while at the same time the answers are waiting on the web. It took me couple of seconds to open google and type in sorting algorithm. The first result sent me to the Wikipedia where everything is explained. Granted, the Wikipedia articles won't give you ready made code, but they'll give you the algorithm, which is the important bit.

http://en.wikipedia.org/wiki/Sorting_algorithm

Barney

sapero

declare cdecl extern _qsort(pointer array,int elementcount,int elementsize,int callback)
declare cdecl extern _system(pointer command)
declare cdecl sortIntCB(pointer q, pointer w),int

int array[20]

' fill array with random numbers, then print it
for a=0 to 19
array[a] = rand(10,99)
print array[a],
next a
print

' sort the array
_qsort(array, 20, 4, &sortIntCB)

' display sorted
for a=0 to 19
print array[a],
next a
print

' wait key
_system("pause")


sub sortIntCB(pointer q, pointer w),int
if (*<int>q = *<int>w) then return 0
if (*<int>q > *<int>w) then return 1
return -1
endsub

hugh

Thank you Sapero.
Thought i would let everyone know, we are moving house on monday 1/12/08
everything is packed, except the laptop, i have a multitude of old books on Basic, with sample programs.
I used to know the algorithm of by heart.
why should i Google it?, Is the forums not for this very thing?.
I am a patient man,  i dont mind if anyone think its easy to do what i requested help on.
incidently Barny, if you had have just typed the algorithm in, you would not have ,"wasted good keyboard ink"  :)
Oh and thank you as well Barney.

Regards

Hugh