Posts: 54
Threads: 16
Joined: Jan 2003
Maybe this is a stupid question.... :???:
I'm asking it anyway. Why doesn't it work?
I want the words putted in printed in alfabetic order.
Code:
10 input a$(i)
if a$(i)= "stop" then goto 20
i=i+1
goto 10
20 let n=i
let i=1
goto 30
25
let i=1
let t=t+1
let k=k+1
let i=1+t
if i=n then goto 40
30
a$(k)=a$(i)
i=i+1
if a$(i)<a$(k) then let a$(i)=a$(k)
if a$(i)=n then goto 25
goto 30
40 for c = 1 to k
print c, a$(k)
next c
Posts: 66
Threads: 32
Joined: Dec 2002
I really don't understand your code.. But i guess you are trying to work it out by checking the ascii code.. If you have some strings and take out the first characters, make them lower or uppercase and sort the ascii values of them..
Otherwise if you are like me, lazy
there is always the dos command sort:
sort "sourcefile" > "targetfile"
good luck
ttp://hem.passagen.se/qb.basta
Posts: 970
Threads: 77
Joined: Jun 2002
there's that or you could do something like a bubblesort or quicksort...
Code:
Bubble Sort
While Flag$ <> "yes"
Flag$ = "yes"
For x = 1 to 'amount of words/numbers needed to be sorted minus 1
If Ucase$(Word(x)) > Ucase$(Word(X+1)) then
Swap Word(x), Word(x+1)
Flag$ = "no"
end if
next x
wend
That should do any type of sorting for Words or Number arrays for you.
Posts: 3,368
Threads: 195
Joined: Jan 2003
But you shouldn't use bubblesort, or so they say.
Bubble sort takes exponential running time = n^2. Quicksort = n log n. I'm SOOOOOO GOOD MAKE ME KING YESYESYES...
Anyways, there is a quicksort example in that ABC website.
Quicksort is much longer to write though.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 3,288
Threads: 167
Joined: Nov 2001
Me and my address book....
http://relsoft.wrq.cjb.net/files/Address.bas
Check out the SortIndex sub....
Uses ShellSort...
;*)_
Posts: 54
Threads: 16
Joined: Jan 2003
Thanks for the help, but it didn't really make me understand qb better. The commands you're suggesting are far to complex (yet...) I'm really a beginner, and what I meant was really simple. I think I can get it out of a book somewhere.....some time.
Now I'm posting another question, doesn't have anything to do with this one.... ;-)
Posts: 970
Threads: 77
Joined: Jun 2002
that is why i displayed the bubble sort. It's one of the better sorting routines to learn first because it is so easy. If you wanted i could explain how it works for you. From that you can move onto something like quicksort or other sorts like that. Wasn't there a sorting sample program that came with one of the versions of qbasic and was made by microsoft...it had like examples of a few different sorting methods and it sorted the green bars by their lengths. Showed how fast each sorting method was and what they did. I believe i remember seeing that somewhere before.
Posts: 3,368
Threads: 195
Joined: Jan 2003
ABC Archives.
Here's a bubble sort:
DIM SHARED array1%(100)
'make the array
for I% = 1 to 100
array1%(I%) = INT(RND * 500)
next I%
'now sort it
do
n% = 0
FOR I% = 1 to 99
IF array1%(I%) > array1%(I% + 1) THEN SWAP array1%(I%), array1%(I%+1): n% = 1
NEXT I%
loop until n% = 0
:bounce:
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 3,368
Threads: 195
Joined: Jan 2003
here's another one. not sure how it's called. Halves the bubble sort time.
FOR I% = 1 TO 100
FOR j% = I% TO 100
IF array1%(I%) > array1%(j%) THEN SWAP array1%(I%), array1%(j%)
n1% = n1% + 1
NEXT j%
NEXT I%
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 6,419
Threads: 74
Joined: Mar 2002
It is a hack to bubblesort to achieve more speed. When I was taught bubblesort, I was taught that algorithm you posted.