Simple table sorting. :)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Simple table sorting. :)

Post by mrminecrafttnt »

Code: Select all

dim as integer value(30)
randomize timer
for i as integer = 0 to ubound(value)
    value(i) = Int(rnd*9999)
next


for i as integer = 0 to ubound(value)
    for i2 as integer  = 0 to ubound(value)
        if value(i) < value(i2) then
            swap value(i),value(i2)
        end if
    next
next

for i as integer = 0 to ubound(value)
    print value(i)
next
sleep


    
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Simple table sorting. :)

Post by D.J.Peters »

Sorry but this looks not really clever it's a runtime of N x N ;-)

How ever it should be:
if (i<>i2) andalso (value(i) < value(i2)) then swap value(i),value(i2)

Joshy

Code: Select all

dim as integer value(30)
randomize timer
for i as integer = 0 to ubound(value)
  value(i) = Int(rnd*9999)
next
for i as integer = 0 to ubound(value)
  print value(i),
next
print : print

for i as integer = 0 to ubound(value)
  for i2 as integer = 0 to ubound(value)
    if (i<>i2) andalso (value(i) < value(i2)) then swap value(i),value(i2)
  next
next

for i as integer = 0 to ubound(value)
   print value(i),
next
print : print 
print "done ..."
sleep
Post Reply