Jump to content
IGNORED

Fast Simple Combsort in BASIC


TheBF

Recommended Posts

  • 3 months later...

Cool!!

 

The 12hr runtime with no completion that you mention really shows the problems with Bubble sorting large numbers of records.

Exponential expansion at its finest.

 

I should do a comparison of your gap value of 2 versus 1.3 that comb sort recommends.

I have some demo code that should make that pretty simple. I will publish results here when I get something.

Link to comment
Share on other sites

FWIW, the C++ version of that software took about 5 minutes.
The quicksort helped a little, but the language made a huge difference.. :D

Bubble sort sucks because doesn't move data very far per pass.
My version moves it as far as possible thanks to the gap.
Small mods to the loops could make it function exactly like comb sort.
If I calculate the maximum number of passes where data can be swapped at each level, I could eliminate passes where no swaps take place.
That's the Q loop. Just change the start value, the step to -1, and change Q to zero if no swaps take place.
An else would do that last part, but regular basic on the MC-10 doesn't have ELSE.
But what is the theoretical number of passes?
I was going to try N-1 (divide # in list by 2 and you use 1, by 4 and you use 3, 8 & 7, etc...) but the first attempt was messed up, so I cleaned up the loops and left that for another day.

This is one of those tasks people started running and came back later.
I should time how long each pass takes.
For a 0.89 MHz machine to sort 850+ names that are often very similar in 10 minutes... I'd say that's acceptable.
If I ditch the data I'm not using, I could probably fit all 1000 names in that array.

  • Like 1
Link to comment
Share on other sites

So I have an onscreen sort of the Dutch flag problem, I just pulled up the numbers in the source code

 

RNDSCREEN START COMBSORT STOP. WAIT \ 00:12.28
RNDSCREEN START BUBBLE STOP. WAIT \ 06:00.15 :-D

 

When I replaced the /1.35 with divide by 2 (which is very fast in forth because it's one shift instruction)

 

The sort was fast out of the gate, but quickly degenerated to a bubble sort because the GAP became 1.

 

This made the results very dependant on input data.

To convert a Russian flag (BLUE WHITE RED) to a Dutch flag (RED WHITE BLUE) it was only 7.5 secs

 

But when the input was random red,white and blue characters it took 3 mins, 51 seconds.

 

So it looks like a fractional GAP of GAP=GAP/1.3 is generally better than GAP=GAP/2 but not for all cases.

Link to comment
Share on other sites

So I have an onscreen sort of the Dutch flag problem, I just pulled up the numbers in the source code

 

RNDSCREEN START COMBSORT STOP. WAIT \ 00:12.28

RNDSCREEN START BUBBLE STOP. WAIT \ 06:00.15 :-D

 

When I replaced the /1.35 with divide by 2 (which is very fast in forth because it's one shift instruction)

 

The sort was fast out of the gate, but quickly degenerated to a bubble sort because the GAP became 1.

 

This made the results very dependant on input data.

To convert a Russian flag (BLUE WHITE RED) to a Dutch flag (RED WHITE BLUE) it was only 7.5 secs

 

But when the input was random red,white and blue characters it took 3 mins, 51 seconds.

 

So it looks like a fractional GAP of GAP=GAP/1.3 is generally better than GAP=GAP/2 but not for all cases.

 

I haven't tried 1.3, but I tried 1.7 which was faster than 2, and starting with a larger gap than dividing by 2 may be a little faster for out of order data..

Without eliminating the last pass at each gap, it has no chance of being faster than the comb sort, and even then it's probably not as fast.

It's more of an optimized bubbles sort, and I'm going to make a few other tests to see if a hunch is true.

*edit*

1.7 Was faster than 1.3 by quite a bit, but 1.5 seems to be faster than 1.7.

*edit*

1.5 is faster than 1.4 and 1.6... at least with my data.

*edit*

After some additional testing, 30+% of all passes through the data were a waste, and calculating a theoretical maximum number of passes is useless.

It's about as optimized as it can get. Also, the optimization that reduces the number of items compared in a pass rarely does much before the final pass on my data.

Edited by JamesD
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...