Qbasicnews.com
February 22, 2020, 08:18:26 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: alfabetic order  (Read 6555 times)
someone42
Member
*
Posts: 44


« Reply #15 on: February 18, 2003, 05:38:06 AM »

Quote
Hey,
at the moment we must code those sort progs in school.
But not in QB or C ... NO ... in LOGO


I feel sorry for you.

Have you considered asking the teacher what the whole point of this is? You're probably going to spend more time writing hacks to get logo to do stuff than figuring out how sorting works. Sad
Logged
banAnnA
Member
*
Posts: 56



« Reply #16 on: February 18, 2003, 07:08:54 AM »

Well, I finally did it. I feel kind of stupid right now, was making the same mistake all the time......
But this is clear to me now.

Quote from: "Agamemnus"

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%


the c code is far to complicated for me, so I wont try any of the examples given. I'll leave that with you (you refers to all experienced programmers and -wannabees....).  Apart from that, it doesn't work in qb, so why should I try? Anyway, thanks for the tips.
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #17 on: February 18, 2003, 11:14:30 AM »

here's a truncated quicksort that I stole (and truncated, greatly) from a site. It uses recursion, though. That is not as efficient as iteration with arrays. Perhaps someone could make it iterative with arrays. Sad

Code:

DECLARE SUB DoSort (SortArray() AS INTEGER, Low AS INTEGER, High AS INTEGER)
array.max% = 250
DIM array1(array.max%) AS INTEGER
DIM number AS INTEGER
'generate a random array.
RANDOMIZE TIMER
CLS : FOR I% = 1 TO array.max%: array1(I%) = INT(RND * 5000) + 1: NEXT I%
DoSort array1(), 1, array.max%
'print the sorted array.
CLS : PRINT "Final sorted array :": PRINT
FOR I% = 1 TO array.max%: PRINT array1(I%); : NEXT I%

SUB DoSort (SortArray() AS INTEGER, Low AS INTEGER, High AS INTEGER)
DIM Lower AS INTEGER: DIM Higher AS INTEGER
DIM RandIndex AS INTEGER: DIM Partition AS INTEGER
IF Low < High THEN
IF High - Low = 1 THEN
IF SortArray(Low) > SortArray(High) THEN
SWAP SortArray(Low), SortArray(High)
END IF
ELSE
RandIndex = INT(RND * (High - Low + 1)) + Low
SWAP SortArray(High), SortArray(RandIndex)
Partition = SortArray(High)
DO
Lower = Low
Higher = High
DO WHILE (Lower < Higher) AND (SortArray(Lower) <= Partition)
Lower = Lower + 1
LOOP
DO WHILE (Higher > Lower) AND (SortArray(Higher) >= Partition)
Higher = Higher - 1
LOOP
IF Lower < Higher THEN SWAP SortArray(Lower), SortArray(Higher)
LOOP WHILE Lower < Higher
SWAP SortArray(Lower), SortArray(High)
IF (Lower - Low) < (High - Lower) THEN
DoSort SortArray(), Low, Lower - 1: DoSort SortArray(), Lower + 1, High
ELSE
DoSort SortArray(), Lower + 1, High: DoSort SortArray(), Low, Lower - 1
END IF
END IF
END IF
END SUB
Logged

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.
hedgehog
New Member

Posts: 11


« Reply #18 on: February 28, 2003, 04:52:49 AM »

'Here I have the words loaded as a DATA script, but it's easily 'modifiable if you wanted to be prompted for the words.

REM Bubble sort
REM Create flags
SCREEN 12
CONST True% = 1
CONST False% = 0

DIM friends$(15)
DATA mary, tom, dick, adam, zeke, paul, stanley, stuart, maude, maryjane, marie, maryellen, maria, matilda, marjorie
FOR load.subscript = 1 TO 15 STEP 1
 READ friends$(load.subscript)
NEXT load.subscript

CLS
COLOR 1
PRINT "Out of order"
LINE (0, 14)-(95, 14), 2
COLOR 15
FOR i = 1 TO 15
 PRINT friends$(i)
NEXT i
SLEEP
DO
OutOfOrder = False%
FOR i = 1 TO 15 - 1
 IF UCASE$(friends$(i)) > UCASE$(friends$(i + 1)) THEN
  SWAP friends$(i), friends$(i + 1)
  OutOfOrder = True%
 END IF
NEXT i
PRINT
LOOP WHILE OutOfOrder
LOCATE 1, 20
COLOR 1
PRINT "Alphabetized"
LINE (150, 14)-(246, 14), 2
COLOR 15
FOR i = 1 TO 15
  LOCATE (i + 1), 20
  PRINT friends$(i)
 NEXT i
END
Logged

n a world as crazy as this one, it ought to be easy to find something that happens solely by chance. It isn't.

Kevin McKeen
The Orderly Pursuit of Pure Disorder.
Discover, January, 1981
seph
Na_th_an
*****
Posts: 1915



« Reply #19 on: October 03, 2003, 02:59:26 PM »

Quote from: "sumojo"
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.


Thanks man, that method really helped me out (although I find it works better without UCASE$())
Logged

earn.
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #20 on: October 03, 2003, 08:27:22 PM »

Aga, you posted a 9 line iteritave quicksort in the challenge section a long time ago...
Logged

Agamemnus
x/ \z
*****
Posts: 3491



« Reply #21 on: October 06, 2003, 10:20:44 PM »

Yes, that's true. However, notice the date of that post......
Logged

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.
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #22 on: October 06, 2003, 11:55:54 PM »

The time you made it makes it invalid?
Logged

Agamemnus
x/ \z
*****
Posts: 3491



« Reply #23 on: October 07, 2003, 12:04:14 PM »

I posted that before I posted the one in the challenges Forum....... :|
Logged

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.
david9c
New Member

Posts: 2


« Reply #24 on: March 29, 2004, 04:56:18 AM »

I'm still looking for a text description of quick sort, but if you're
looking for a game send me an email. I have the greatest
Keno game ever.
d12connelly@yahoo.com
Logged
Z!re
*/-\*
*****
Posts: 4599


« Reply #25 on: March 29, 2004, 09:28:05 AM »

Last post: October 07 2003

Your post: March 29 2004


Stop posting everywhere, posting it once is enough... This just makes people mad at you....

*smacks david9c...* Reviver.. pfffft.... :lol:
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #26 on: April 10, 2004, 10:32:36 PM »

BanAnnA,
Sumojo's "bubble" sort looks pretty good, although a little inefficient, purely from a aesthetic point of view. For sorting small amounts of data, say 100-200 elements, its a nice solution. Actually it's not a bubble but a sift sort.

Do you know what a bubble sort does and why it's called a "bubble" sort? The whole idea is to start at one end of the list to be sorted, and compare adjacent list elements. In the case of a bubble sort, you start at the bottom, compare adjacent elements, swap them if the one nearest the top of the list is greater. If you did a swap you set a switch which means "I did a swap so some elements are not in order". Basically, you keep doing this until the lowest element "bubbles" up to the top of the list. When you go through the list and don't need to do a swap, then the entire list is in order.

Most people, like Sumojo, prefer to process the list starting from the top. This then is called a "sift" sort because the highest element "sifts" down to the bottom of the list. The implementation is fundamentally the same as a bubble.

The bubble and sift are the simplest to understand. You should try to understand these to begin with. Take Sumojo's code and imbed it into a test program a run tests on it until you understand. Forget Quicksort for now, it will confuse you.
Good luck.
*****
Logged
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!