The QBasic Forum      Other Subforums, Links and Downloads
 Return to Index  

Interesting variation of partial shuffle sort

April 9 2008 at 7:39 PM
Solitaire  (Login Solitaire1)
from IP address 74.73.58.57


Response to For Example

I revised Montrealer's code a bit and added comments:


'by Montrealer  (cleaned up & variables renamed)
DIM picks AS INTEGER, range AS INTEGER, last AS INTEGER
DIM index AS INTEGER, x AS INTEGER
CLS
RANDOMIZE TIMER
range = 40                              'entire range
picks = 10                              'number of selections
DIM numbers(range) AS INTEGER

FOR x = 1 TO range                      'assign sequential values to array
    numbers(x) = x
NEXT x

PRINT "Partial Shuffle Sort:"
PRINT
last = range                            'range will be temporarily diminished
FOR x = 1 TO picks                      'only shuffles up to the number of selections
    index = INT(RND * last) + 1         'random index up to diminished range
    PRINT TAB(10); numbers(index), "goes to position:"; last
    SWAP numbers(index), numbers(last)  'swaps value with end of diminished range
    last = last - 1
NEXT x

PRINT : PRINT "Numbers selected, in sequence:"
FOR x = range TO last + 1 STEP -1
    PRINT numbers(x);
NEXT x

PRINT : PRINT
PRINT "Numbers not selected:"
FOR x = 1 TO last
    PRINT numbers(x);
NEXT x
SYSTEM


 
 Respond to this message   
Responses