Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
alfabetic order
#1
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
Reply
#2
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 Big Grin there is always the dos command sort:

sort "sourcefile" > "targetfile"

good luck
ttp://hem.passagen.se/qb.basta
Reply
#3
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.
url=http://fileanchor.com]FileAnchor[/url] - ImageAnchor - FBTK - QbasicNews - VPlanet - Various
Reply
#4
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.
Reply
#5
Me and my address book....

http://relsoft.wrq.cjb.net/files/Address.bas

Check out the SortIndex sub....

Uses ShellSort...

;*)_
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#6
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.... ;-)
Reply
#7
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.
url=http://fileanchor.com]FileAnchor[/url] - ImageAnchor - FBTK - QbasicNews - VPlanet - Various
Reply
#8
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.
Reply
#9
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.
Reply
#10
It is a hack to bubblesort to achieve more speed. When I was taught bubblesort, I was taught that algorithm you posted.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)