Author Topic: I can't decide which I like best...  (Read 6345 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #15 on: September 23, 2020, 12:10:39 am »
From reply #2
Code: QB64: [Select]
  1. WIDTH 144, 25
  2. j = 0
  3.     READ mydata$
  4.     IF mydata$ = "EOF" THEN EXIT DO
  5.     j = j + 1
  6. DIM myarray$(j)
  7.  
  8.     READ mydata$
  9.     IF mydata$ = "EOF" THEN EXIT DO
  10.     j = 1: occ% = 0: h = h + 1
  11.     dataconcat$ = mydata$ + "|" + dataconcat$
  12.     DO UNTIL j > LEN(dataconcat$)
  13.         IF INSTR(j, dataconcat$, mydata$ + "|") - j = 0 THEN occ% = occ% + 1
  14.         j = INSTR(j, dataconcat$, "|") + 1
  15.     LOOP
  16.     myarray$(h) = mydata$ + "#" + LTRIM$(STR$(occ%))
  17. dataconcat$ = ""
  18. FOR i = 1 TO h
  19.     FOR j = 1 TO h
  20.         IF i <> j THEN
  21.             IF myarray$(i) < myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  22.         END IF
  23. NEXT j, i
  24. FOR i = 1 TO h
  25.     PRINT myarray$(i); ", ";
  26. PRINT "End Method #1."
  27.  
  28. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog
  29.  

Trimmed reply #2 down a bit:
Code: QB64: [Select]
  1. DIM myarray$(1 TO 1000) ' some over the top number to avoid counting and then DIM
  2.     READ mydata$
  3.     IF mydata$ = "EOF" THEN EXIT DO
  4.     nItems = nItems + 1: myarray$(nItems) = mydata$
  5. FOR i = 1 TO nItems - 1 'nice sort routine, does this always work?
  6.     FOR j = i + 1 TO nItems
  7.         IF myarray$(i) > myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  8. NEXT j, i
  9. FOR i = 1 TO nItems ' if we really must number the animals
  10.     IF i = 1 THEN
  11.         lastWord$ = myarray$(i): cnt = 1
  12.     ELSE
  13.         IF myarray$(i) = lastWord$ THEN cnt = cnt + 1 ELSE cnt = 1: lastWord$ = myarray$(i)
  14.     END IF
  15.     myarray$(i) = lastWord$ + "#" + _TRIM$(STR$(cnt))
  16.     IF i = nItems THEN PRINT myarray$(i) ELSE PRINT myarray$(i); ", ";
  17. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog,EOF
  18.  

I modified your sort routine, man that is short and sweet! Does it always work?
« Last Edit: September 23, 2020, 12:14:56 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #16 on: September 23, 2020, 12:31:31 am »
Looks cute:
Code: QB64: [Select]
  1. DIM b(1 TO 480) 'lilSort test b+ 2020-09-23
  2.     nItems = INT(RND * (480)) + 1
  3.     FOR i = 1 TO nItems
  4.         b(i) = INT(RND * 100)
  5.     NEXT
  6.     lilSort b(), nItems
  7.     FOR i = 1 TO nItems
  8.         PRINT b(i);
  9.     NEXT
  10.     SLEEP
  11.     CLS
  12.  
  13. SUB lilSort (arr(), topItem)
  14.     DIM i AS INTEGER, j AS INTEGER
  15.     FOR i = 1 TO topItem - 1
  16.         FOR j = i + 1 TO topItem
  17.             IF arr(i) > arr(j) THEN SWAP arr(i), arr(j)
  18.     NEXT j, i
  19.  
  20.  

In action, I suppose it is about the worst in efficiency:
Code: QB64: [Select]
  1. DIM b(1 TO 480) 'lilSort test b+ 2020-09-23
  2.     nItems = INT(RND * (480)) + 1
  3.     FOR i = 1 TO nItems
  4.         b(i) = INT(RND * 100)
  5.     NEXT
  6.     lilSort b(), nItems
  7.     CLS
  8.     FOR i = 1 TO nItems
  9.         PRINT b(i);
  10.     NEXT
  11.     _DISPLAY
  12.     SLEEP
  13.     CLS
  14.  
  15. SUB lilSort (arr(), topItem)
  16.     DIM i AS INTEGER, j AS INTEGER
  17.     FOR i = 1 TO topItem - 1
  18.         FOR j = i + 1 TO topItem
  19.             IF arr(i) > arr(j) THEN SWAP arr(i), arr(j)
  20.             GOSUB show
  21.             _LIMIT 800
  22.     NEXT j, i
  23.     EXIT SUB
  24.     show:
  25.     CLS
  26.     FOR k = 1 TO topItem
  27.         PRINT arr(k);
  28.     NEXT
  29.     _DISPLAY
  30.     RETURN
  31.  
  32.  
« Last Edit: September 23, 2020, 12:49:40 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: I can't decide which I like best...
« Reply #17 on: September 23, 2020, 01:46:59 am »
Nice, Mark.

My way to add an array and place the results in alphabetical order would be as follows...

Code: QB64: [Select]
  1. WIDTH 144, 25
  2. j = 0
  3.     READ mydata$
  4.     IF mydata$ = "EOF" THEN EXIT DO
  5.     j = j + 1
  6. DIM myarray$(j)
  7.  
  8.     READ mydata$
  9.     IF mydata$ = "EOF" THEN EXIT DO
  10.     j = 1: occ% = 0: h = h + 1
  11.     dataconcat$ = mydata$ + "|" + dataconcat$
  12.     DO UNTIL j > LEN(dataconcat$)
  13.         IF INSTR(j, dataconcat$, mydata$ + "|") - j = 0 THEN occ% = occ% + 1
  14.         j = INSTR(j, dataconcat$, "|") + 1
  15.     LOOP
  16.     myarray$(h) = mydata$ + "#" + LTRIM$(STR$(occ%))
  17. dataconcat$ = ""
  18. FOR i = 1 TO h
  19.     FOR j = 1 TO h
  20.         IF i <> j THEN
  21.             IF myarray$(i) < myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  22.         END IF
  23. NEXT j, i
  24. FOR i = 1 TO h
  25.     PRINT myarray$(i); ", ";
  26. PRINT "End Method #1."
  27.  
  28. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog
  29.  

Pete


Bubblesort??    AAAAARRRRRGGGHHHHHH!!!  My poor eyes!  My poor programmer’s sensibilities!!  Bubble sort is even worse coding practice than GOTO!!!

And the ABSOLUTE worst version of Bubblesort!  You didn’t even make the inner loop FOR j = i + 1 TO h...

I...  I feel faint!  Sorry guys...  I gotta go barf.  Somebody please teach Pete a decent sort algorithm!  CODEGUY, WE NEED YOU, QUICK!!!

Blaaaarrrrrggggghhhhhh...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: I can't decide which I like best...
« Reply #18 on: September 23, 2020, 02:06:30 am »
Here is my contribution which works quite nicely. When timed on my machine for sorting and rebuilding the array it took 0.31 seconds.

Code: QB64: [Select]
  1. _TITLE "Sort da list"
  2.     READ mydata$
  3.     IF mydata$ = "EOF" THEN EXIT DO
  4.     nItems = nItems + 1
  5.     REDIM _PRESERVE myarray$(nItems)
  6.     myarray$(nItems) = mydata$
  7.  
  8. OPEN "datatosort.txt" FOR OUTPUT AS #1
  9. FOR x = 1 TO UBOUND(myarray$)
  10.     PRINT #1, myarray$(x)
  11.  
  12. SHELL _HIDE "PowerShell Get-Content datatosort.txt | sort | Out-File sorteddata.txt -Encoding ASCII" 'regular sort
  13. 'SHELL _HIDE "PowerShell Get-Content datatosort.txt | sort | Get-Unique | Out-File sorteddata.txt -Encoding ASCII" 'sort with duplicates omitted
  14.  
  15. OPEN "sorteddata.txt" FOR BINARY AS #1
  16. FOR x = 1 TO UBOUND(myarray$)
  17.     IF NOT EOF(1) THEN
  18.         LINE INPUT #1, myarray$(x)
  19.         PRINT myarray$(x)
  20.     END IF
  21.  
  22. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog,EOF

  [ You are not allowed to view this attachment ]    [ You are not allowed to view this attachment ]  

Code: QB64: [Select]
  1. _TITLE "Sort da list"
  2. x# = TIMER(0.01)
  3. SHELL _HIDE "PowerShell Get-Content 9999words.txt | sort | Out-File sorteddata.txt -Encoding ASCII"
  4. 'SHELL _HIDE "PowerShell Get-Content 9999words.txt | sort | Get-Unique | Out-File sorteddata.txt -Encoding ASCII"
  5.  
  6. OPEN "sorteddata.txt" FOR BINARY AS #1
  7.     IF EOF(1) = 0 THEN
  8.         x = x + 1
  9.         REDIM _PRESERVE myarray$(x)
  10.         LINE INPUT #1, myarray$(x)
  11.     END IF
  12. y# = TIMER(0.01)
  13. PRINT y# - x#
The above code along with my attachment "9999words.txt" can be sorted in 0.77 seconds on my machine and if you want only unique you can get it in 0.56 seconds.
« Last Edit: September 23, 2020, 11:57:54 am by SpriggsySpriggs »
Shuwatch!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #19 on: September 23, 2020, 05:00:03 am »
Bad salsa on Taco Tuesday again, Steve? I hope you recover in time for Pon Farr Friday, but in case you don't, check out my fruit sort at QBF sometime. Ah, the good ol' days. Anyway, the speed test isn't for the alphabetizing part, it is for determining which of the two methods of using MID$() or INSTR() work faster for determining duplicates during the concatenation process.

Oh dammit, I think I stepped in some...

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: I can't decide which I like best...
« Reply #20 on: September 23, 2020, 09:12:55 am »
I lolled at Steve's reaction to bubble sort. I felt myself being channeled from somewhere so I had to check the forums for flames, and sure enough... Haha
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #21 on: September 23, 2020, 11:21:10 am »
Come on guys, as Pete says you don't need a ballistic missile to kill a deer specially if you want to eat it after.

Remember this is for Pete's phone calls, you don't need the heavy artillery for a couple dozen items to sort.

.31 secs!!! Ha!

Check this out
Code: QB64: [Select]
  1. start = TIMER(.001)
  2. WIDTH 144, 25
  3. '_SCREENMOVE 0, 0  '<<< try the time commented and not commented out
  4. j = 0
  5.     READ mydata$
  6.     IF mydata$ = "EOF" THEN EXIT DO
  7.     j = j + 1
  8. DIM myarray$(j)
  9.  
  10.     READ mydata$
  11.     IF mydata$ = "EOF" THEN EXIT DO
  12.     j = 1: occ% = 0: h = h + 1
  13.     dataconcat$ = mydata$ + "|" + dataconcat$
  14.     DO UNTIL j > LEN(dataconcat$)
  15.         IF INSTR(j, dataconcat$, mydata$ + "|") - j = 0 THEN occ% = occ% + 1
  16.         j = INSTR(j, dataconcat$, "|") + 1
  17.     LOOP
  18.     myarray$(h) = mydata$ + "#" + LTRIM$(STR$(occ%))
  19. dataconcat$ = ""
  20. FOR i = 1 TO h
  21.     FOR j = 1 TO h
  22.         IF i <> j THEN
  23.             IF myarray$(i) < myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  24.         END IF
  25. NEXT j, i
  26. FOR i = 1 TO h
  27.     PRINT myarray$(i); ", ";
  28. PRINT "End Method #1."
  29. PRINT "Time:"; TIMER(.001) - start
  30. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog
  31.  
  32.  
  33.  

Discovery: the line that takes the most time by far in this program is the _SCREENMOVE !!!

I don't think Pete even cares about the sorting, I brought in the subject because I thought it easier to # the animals after a sort.

BTW I have a version of fruit sort, cool!
Code: QB64: [Select]
  1. DEFINT A-Z
  2. numberOfFruits = 25
  3. DIM Basket$(10)
  4. FOR i = 1 TO numberOfFruits 'create fruits
  5.     Fruit = INT(RND * 9) + 1: f$ = RIGHT$(STR$(Fruit), LEN(STR$(Fruit)) - 1)
  6.     IF i <> numberOfFruits THEN PRINT f$; ", "; ELSE PRINT f$;
  7.     Basket$(Fruit) = Basket$(Fruit) + f$ + ", "
  8. FOR Nut = 1 TO 9
  9.     IF LEN(Basket$(Nut)) THEN
  10.         IF Nut <> 9 THEN PRINT Basket$(Nut); ELSE PRINT LEFT$(Basket$(Nut), LEN(Basket$(Nut)) - 2)
  11.     END IF
  12.  
  13.  
« Last Edit: September 23, 2020, 11:46:21 am by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: I can't decide which I like best...
« Reply #22 on: September 23, 2020, 12:00:17 pm »
.31 secs!!! Ha!
@bplus That is because on smaller data sets, PowerShell will be slower. However, if you look at my updated reply, I can sort 9999 words in 0.77 seconds or 0.56 seconds if I omit duplicates. Try using my list of 9999 words and see how your algorithm does.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #23 on: September 23, 2020, 12:14:52 pm »
@bplus That is because on smaller data sets, PowerShell will be slower. However, if you look at my updated reply, I can sort 9999 words in 0.77 seconds or 0.56 seconds if I omit duplicates. Try using my list of 9999 words and see how your algorithm does.

Sure 9999 words but then I would pull out Qsort and blow that time away and then you will say, "Wait let's try 999,999,999 words then Powershell will win! :-))

I am saying for a couple hundred words even the worst sort is better than jobbing out to Powereshell or whatever Hydrogen Bomb of the sorting world you want to bring in.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #24 on: September 23, 2020, 04:37:23 pm »
That's why I stick with my patented Stupid Sort. It's the quickest, easiest sort code to remember and type into the IDE. I can place it in a program in under 30 seconds. It works plenty fast for most jobs where the lists to sort are 1000 items or less, but you guys probably thought I posted it just to make Steve puke. Nah, that's just a bonus!

Pete

 - If you're out of your mind, see a psychiatrist. If you're just out of sorts, see CodeGuy.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: I can't decide which I like best...
« Reply #25 on: September 23, 2020, 06:38:04 pm »
That's why I stick with my patented Stupid Sort. It's the quickest, easiest sort code to remember and type into the IDE. I can place it in a program in under 30 seconds. It works plenty fast for most jobs where the lists to sort are 1000 items or less, but you guys probably thought I posted it just to make Steve puke. Nah, that's just a bonus!

Pete

 - If you're out of your mind, see a psychiatrist. If you're just out of sorts, see CodeGuy.

That’s why I keep several sorts in my folder, and all I need do is either $INCLUDE them, or copy/paste them into my program.  It’s efficient program-wise, and even faster coding than typing a puke-sort is.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!