trouble with sort
Moderators:Administrator, Global Moderator
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
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
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
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
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.
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
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.
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
This is a good time to practice using SubRoutines also.
They make program flow a little easier to follow:
Setup:
OPTION BASE 1
CLS
v = 5
DIM a(v, 2), b(v)
GOSUB Load
GOSUB Compare
GOSUB Sort
GOSUB PrintList
END
Load:
' 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
RETURN
Compare:
' 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
RETURN
Sort:
' Sort variables.
FOR x = 1 TO v
y = a(x, 2)
b(y) = a(x, 1)
NEXT x
RETURN
PrintList:
' Print sorted list.
FOR x = 1 TO v
PRINT b(x)
NEXT x
RETURN
DATA 6,10,25,5,3
They make program flow a little easier to follow:
Setup:
OPTION BASE 1
CLS
v = 5
DIM a(v, 2), b(v)
GOSUB Load
GOSUB Compare
GOSUB Sort
GOSUB PrintList
END
Load:
' 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
RETURN
Compare:
' 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
RETURN
Sort:
' Sort variables.
FOR x = 1 TO v
y = a(x, 2)
b(y) = a(x, 1)
NEXT x
RETURN
PrintList:
' Print sorted list.
FOR x = 1 TO v
PRINT b(x)
NEXT x
RETURN
DATA 6,10,25,5,3
Re: trouble with sort
thanks a lot for the help.
Re: trouble with sort
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. ;)
Code: Select all
CLS
ArraySize = 500
DIM MyData(1 TO 500)
FOR X = 1 TO 500
MyData(X) = INT(RND * 500)
NEXT
FOR Old = 1 TO ArraySize
New = Old
FOR Test = Old + 1 TO ArraySize
IF MyData(Test) < MyData(New) THEN
New = Test
END IF
NEXT Test
IF New > Old THEN
SWAP MyData(Old), MyData(New)
END IF
NEXT Old
FOR X = 1 TO 500
PRINT MyData(X)
NEXT
Re: trouble with sort
OOPS! I made a couple of typos in that code....
This is corrected...sorry! ::)
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
FOR Old = 1 TO ArraySize
New = Old
FOR Test = Old + 1 TO ArraySize
IF MyData(Test) < MyData(New) THEN
New = Test
END IF
NEXT Test
IF New > Old THEN
SWAP MyData(Old), MyData(New)
END IF
NEXT Old
FOR X = 1 TO ArraySize
PRINT MyData(X)
NEXT