Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
quicksort set
#1
I just finished a set of 20 quicksorts... for all the basic QB types. (string, integer, long, double, single)

http://www.geocities.com/pisforpi/qsort.zip

Also, here's another utility thing: (plz post something..)
http://forum.qbasicnews.com/viewtopic.php?t=4537

I really feel that we should have 100% complete basic stuff for QB before going on to complicated 3D games... but I wouldn't mind if someone just coded a 3D game, either. Wink
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
#2
Mods: Can we put this on the faq?
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#3
It's difficult.

The code is really long.

It would be even longer if all of the code was example 2 instead of example 1.

I could put the framework code, though, since each one has only minor changes...
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
#4
Hey aga...I just looked at your code because...well...just because. I don't want to bash or start another style debate, but I want to offer sincere suggestions that would make your code much more readable. Note that these are not really commands, but rather suggestions/my opinion, even though I've worded them as commands.

1) put your code in order. What I mean is, don't perform a function that's not really needed for another 100 lines. Examples include...CLS as first line of code...why is this here? Is this really the first thing the program does? It looks to me like it creates the arrays firs? Why not call RANDOMIZE TIMER just before the first call to RND? If you do this, then each line is much easier to understand, since it appears in the proper context of the program.

2) Learn to block indent with spaces between blocks.

3) use a *few* comments.

4) This is really important...stop using period as a seperator in variable/function names. It makes it look like you are using nested types (are those possible in QB??)

I believe the advantages of doing this are 3-fold. 1) your code would be easy to read/understand, and therefore others might be willing to criticize the code itself, rather than always being blinded with it's incomprehensibility2)you would get more constructive criticism whereby you could learn/interact more with those who might otherwise be willing to understand your work3)you might become a better programmer who can handle larger, more complex projects as you become more organized.

As an example, I post some code you wrote and then a suggested modification:

Your code:
Code:
DECLARE SUB qsort.linked.string.lowstart (array1() AS STRING, array2() AS INTEGER, a.max%)
CLS
RANDOMIZE TIMER
a.max% = 100
DIM array1(1 TO a.max%) AS STRING
DIM array2(1 TO a.max%) AS INTEGER
FOR i% = 1 TO a.max%
temp% = INT(RND * 10) + 1
array1$(i%) = SPACE$(temp%)
FOR j% = 1 TO temp%
IF INT(RND * 2) THEN
MID$(array1$(i%), j%, 1) = CHR$(INT(RND * 26) + 97)
ELSE
MID$(array1$(i%), j%, 1) = CHR$(INT(RND * 26) + 65)
END IF
NEXT j%
array2(i%) = i%
NEXT i%
t1# = TIMER
qsort.linked.string.lowstart array1(), array2(), a.max%
t2# = TIMER
PRINT t2# - t1#
SLEEP
FOR i% = 1 TO a.max%
PRINT array1(i%)
NEXT i%

Edit::: As an example of why to block indent...the last line of code is Next i%...The first thing I did was look what this variable was...and I see the first meat of your code is a FOR i% loop, and I said-A-Ha!!!...but alas, I was wrong Sad

1-way to make it more readable:
Code:
DECLARE SUB QsortLinkedStringLowstart (array1() AS STRING, array2() AS INTEGER, arraySize)

arraySize = 100
DIM array1(1 TO arraySize) AS STRING
DIM array2(1 TO arraySize) AS INTEGER

'Fill arrays with rand data to be sorted
RANDOMIZE TIMER
FOR i% = 1 TO arraySize    
  temp% = INT(RND * 10) + 1
  array1$(i%) = SPACE$(temp%)
  
  FOR j% = 1 TO temp%
    IF INT(RND * 2) THEN
      MID$(array1$(i%), j%, 1) = CHR$(INT(RND * 26) + 97)  'LowerCase Letter
    ELSE
      MID$(array1$(i%), j%, 1) = CHR$(INT(RND * 26) + 65)  'UpperCase letter
    END IF
  NEXT j%
  
  array2(i%) = i%
NEXT i%

'sort it!!
t1# = TIMER
QsortLinkedStringLowstart array1(), array2(), arraySize
t2# = TIMER

CLS: PRINT "The array of "; ArraySize ; " elements was sorted in ";t2# - t1#;" seconds"
SLEEP

FOR i% = 1 TO arraySize
  PRINT array1(i%)
NEXT i%
END

Cheers...HTH
Reply
#5
Quote:1) put your code in order. What I mean is, don't perform a function that's not really needed for another 100 lines. Examples include...CLS as first line of code...why is this here? Is this really the first thing the program does? It looks to me like it creates the arrays firs? Why not call RANDOMIZE TIMER just before the first call to RND? If you do this, then each line is much easier to understand, since it appears in the proper context of the program.

Standard procedure. CLS and RANDOMIZE TIMER go first. Easy to find. The rest follows. What if I wanted to remove everything and try some other sort or PRINT statement? I wouldn't have to retype CLS and RANDOMIZE TIMER, then.

Quote:2) Learn to block indent with spaces between blocks.

There is nothing to learn. Indentation is unnecessary. AGAIN, code isn't meant to be skipped through as some sort of long, boring, arduous literature book that one has no interest in reading, with the spaces and comments meant to highlight "the good parts". The sample code is really quite simple...

Quote: 3) use a *few* comments.

Again, unnecessary.. maybe it would be necessary if the code consisted of strange functions that aren't immediately apparent.

Quote:4) This is really important...stop using period as a seperator in variable/function names. It makes it look like you are using nested types (are those possible in QB??)

Again, using periods as separators when they're not TYPEs isn't bad. QB also supports line numbers and GOTO statements, and it supports TYPEs. So you can't say that it's bad just because "i look like" I'm doing something.

Besides, what's easier to do -- dot or SHIFT+capital letter? Dot. One hand. And, less error-prone.

Quote:I believe the advantages of doing this are 3-fold. 1) your code would be easy to read/understand, and therefore others might be willing to criticize the code itself, rather than always being blinded with it's incomprehensibility2)you would get more constructive criticism whereby you could learn/interact more with those who might otherwise be willing to understand your work3)you might become a better programmer who can handle larger, more complex projects as you become more organized.

I don't think so. 1), I don't need anyone to criticize my code. 2) I don't *need* people to understand anything about "my work", if they can't read it, they can just use it anyways... (see 1) 3) I *am* organized, and my coding style is a testament to this. However I'm impatient, and the amount of dead games that I started but never finished leaves a blazing trail behind me as I walk.

Quote:Edit::: As an example of why to block indent...the last line of code is Next i%...The first thing I did was look what this variable was...and I see the first meat of your code is a FOR i% loop, and I said-A-Ha!!!...but alas, I was wrong

Which is exactly why Internet Explorer is so vulnerable. There are no easy ways out of reading code. Forget indentation. NOW.

PS: You just might get an error when running that code 'cuz Arraysize isn't an integer...
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
#6
OK.
Reply
#7
No juice left for a battle, eh, FRUIT? Smile
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
#8
I personally have zero difficulty looking through Agamemnus' code...as a former "King Of Spaghetti Code", it's easily readable to me Smile
I'd knock on wood, but my desk is particle board.
Reply
#9
whoah! I thought I had you figured out, and you go and surprise me...
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
No one ever has me figured out, any anyone who thinks they have simply doesn't know enough yet Wink

Anyways, good code man.
I'd knock on wood, but my desk is particle board.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)