Page 1 of 1

trouble with sort

Posted: Tue Mar 16, 2004 3:49 am
by Jonathon
I am having trouble with a sort function that I was working on. I am just starting out with qbasic. Any help would be great.


CLS
a(1) = 6
a(2) = 10
a(3) = 25
a(4) = 5
a(5) = 3

FOR t = 1 TO 5
IF a(t) > a(t + 1) THEN
temp = a(t)
a(t) = a(t + 1)
a(t + 1) = temp
END IF
NEXT t

Re: trouble with sort

Posted: Tue Mar 16, 2004 10:29 am
by yacinator
ok jonathon the simplest sort is the buble sort. it sorts by comparing neighboring variables and swaping them if they're out of order. it's not very efficient when using lists above 30 items. u program it like this

for a=1 to 4
for b=1 to 5
if var(b) < var(b+1) then
swap var(b) , var(b+1)
end if
next b
next a

also jonathon u need to use an array with the sort. if u just have variables a(1), a(2) and so on; if u have something like a(i) (a being a variable) qb will think that the name of the variable is a(i)

if u r serious about learing qb then get a book like qb for dummies or something of that sort

Re: trouble with sort

Posted: Tue Mar 16, 2004 2:30 pm
by Artie
Hi Jonathon;  Here's an example of a technique that I use:

First, I use the "Option Base 1" command because I like my arrays to start with 1 instead of zero.  Then, create a 2-dimension variable. The first element stores the actual value, the second element stores its position in the line.
Next, I'm going to simply compare each value with every other value, and count how many values are "less" than my test value.  If there are 10 numbers less than my test value, the test value must be 10 + 1, or the eleventh value in the line.

OPTION BASE 1
t = 5 ' How many variables you'll have
DIM a(5, 2)
FOR x = 1 TO t
READ a(x, 1) ' Get the variables value
a(x, 2) = 0 ' Initialize the place holder to zero
NEXT x
count = 0
FOR x = 1 TO t
FOR y = 1 to t
if a(x, 1) < a(y, 1) then count = count + 1
NEXT y
a(x, 2) = count
count = 0
NEXT x

I gotta run to work, but this will get you started.
If I can, I'll post a complete working code from work, or later today.

Re: trouble with sort

Posted: Tue Mar 16, 2004 3:32 pm
by Artie
Ok, this code works:
Also, note that in this example, I'm counting the number of variables greater than, rather than less than as in the above example.

Setup:
 OPTION BASE 1
 CLS
 v = 5
 DIM a(v, 2), b(v)
 ' Load variables and initialize position variable to zero.
 FOR q = 1 TO v
   READ t
   a(q, 1) = t
   a(q, 2) = 0
 NEXT q
 ' Compare values.
 FOR x = 1 TO v
   count = 0
   FOR y = 1 TO v
     IF a(x, 1) >= a(y, 1) THEN count = count + 1
   NEXT y
   a(x, 2) = count
 NEXT x
 ' Sort variables.
 FOR x = 1 TO v
   y = a(x, 2)
   b(y) = a(x, 1)
 NEXT x
 ' Print sorted list.
 FOR x = 1 TO v
   PRINT b(x)
 NEXT x
 END

DATA 6,10,25,5,3

You can make "v" any number you want, just make sure you have a matching number of DATA elements.

Re: trouble with sort

Posted: Tue Mar 16, 2004 3:45 pm
by Artie
This is a good time to practice using SubRoutines also.
They make program flow a little easier to follow:

Setup:
&nbsp;OPTION BASE 1
&nbsp;CLS
&nbsp;v = 5
&nbsp;DIM a(v, 2), b(v)
&nbsp;GOSUB Load
&nbsp;GOSUB Compare
&nbsp;GOSUB Sort
&nbsp;GOSUB PrintList
&nbsp;END

Load:
&nbsp;' Load variables and initialize position variable to zero.
&nbsp;FOR q = 1 TO v
&nbsp; &nbsp;READ t
&nbsp; &nbsp;a(q, 1) = t
&nbsp; &nbsp;a(q, 2) = 0
&nbsp;NEXT q
&nbsp;RETURN

Compare:
&nbsp;' Compare values.
&nbsp;FOR x = 1 TO v
&nbsp; &nbsp;count = 0
&nbsp; &nbsp;FOR y = 1 TO v
&nbsp; &nbsp; &nbsp;IF a(x, 1) >= a(y, 1) THEN count = count + 1
&nbsp; &nbsp;NEXT y
&nbsp; &nbsp;a(x, 2) = count
&nbsp;NEXT x
&nbsp;RETURN

Sort:
&nbsp;' Sort variables.
&nbsp;FOR x = 1 TO v
&nbsp; &nbsp;y = a(x, 2)
&nbsp; &nbsp;b(y) = a(x, 1)
&nbsp;NEXT x
&nbsp;RETURN

PrintList:
&nbsp;' Print sorted list.
&nbsp;FOR x = 1 TO v
&nbsp; &nbsp;PRINT b(x)
&nbsp;NEXT x
&nbsp;RETURN

DATA 6,10,25,5,3

Re: trouble with sort

Posted: Tue Mar 16, 2004 5:33 pm
by Jonathon
thanks a lot for the help. :)

Re: trouble with sort

Posted: Thu Apr 01, 2004 10:19 am
by Guest
This is an exchange sort. It's not the fastest, but it's faster than a bubble sort. A quick sort can confuse some people, so I posted this because it's easier to figure out. &nbsp;;)

Code: Select all

CLS
ArraySize = 500

DIM MyData(1 TO 500)
 
FOR X = 1 TO 500
 MyData(X) = INT(RND * 500)
NEXT



 &nbsp; FOR Old = 1 TO ArraySize
 &nbsp; &nbsp;New = Old
 &nbsp; &nbsp; FOR Test = Old + 1 TO ArraySize
 &nbsp; &nbsp; &nbsp;IF MyData(Test) < MyData(New) THEN
 &nbsp; &nbsp; &nbsp; New = Test
 &nbsp; &nbsp; &nbsp;END IF
 &nbsp; &nbsp; NEXT Test

 &nbsp; &nbsp; &nbsp;IF New > Old THEN
 &nbsp; &nbsp; &nbsp; SWAP MyData(Old), MyData(New)
 &nbsp; &nbsp; &nbsp;END IF
 &nbsp; NEXT Old


FOR X = 1 TO 500
 PRINT MyData(X)
NEXT

Re: trouble with sort

Posted: Thu Apr 01, 2004 10:23 am
by Guest
OOPS! I made a couple of typos in that code....

This is corrected...sorry! ::)

Code: Select all

CLS
ArraySize = 500

DIM MyData(1 TO ArraySize)
 
FOR X = 1 TO ArraySize
 MyData(X) = INT(RND * 1000)
NEXT



 &nbsp; FOR Old = 1 TO ArraySize
 &nbsp; &nbsp;New = Old
 &nbsp; &nbsp; FOR Test = Old + 1 TO ArraySize
 &nbsp; &nbsp; &nbsp;IF MyData(Test) < MyData(New) THEN
 &nbsp; &nbsp; &nbsp; New = Test
 &nbsp; &nbsp; &nbsp;END IF
 &nbsp; &nbsp; NEXT Test

 &nbsp; &nbsp; &nbsp;IF New > Old THEN
 &nbsp; &nbsp; &nbsp; SWAP MyData(Old), MyData(New)
 &nbsp; &nbsp; &nbsp;END IF
 &nbsp; NEXT Old


FOR X = 1 TO ArraySize
 PRINT MyData(X)
NEXT