Re: TIB: Back to TI-BASICs


[Prev][Next][Index][Thread]

Re: TIB: Back to TI-BASICs




Jody wrote:
[...]

> Okay, so how did you do it? Just thinking off the top of my head, I
> think I would do something like this. (on an 83)
> :{1,2,3,4,5,6........51,52->L1
> :randInt(1,100,52->L2
> :SortA(L2,L1

This is a very good way to do it, I did not know that SortA could be
used like that (I have never used the command). I did a shuffling
program earlier here at the university, and as the language didn't have
a function like SortA, it never came up, so I just assumed that my
method was the most efficient one.
My guess is that yours is a lot faster though :-)

My algorithm looks (looked? :-) like this:

Func
Local tempdeck,size,i,j
dim(deck)->size
newList(size)->tempdeck
For i,0,size-1,1
 rand(size-i)->j
 deck[j]->tempdeck[i+1]
 deck[size-i]->deck[j]
EndFor
tempdeck
EndFunc

(Indented for your reading pleasure ;-)

In effect, it picks a random element from the 'sorted' list and puts it
in the first available position in the 'shuffled' list.
It then copies the last element of the 'sorted' list into the position
the random element came from, and truncates the last element of the
'sorted list.
Actually it doesn't really truncate, it just moves a pointer one element
down.

-- 
          Rene Kragh Pedersen
------------------------------------------------------------------
man: Why did you get a divorce?
man:: Too many arguments.


Follow-Ups: References: