Computer Science Canada How to Bucket Sort in Turing? |
Author: | turingmachine [ Sat Nov 26, 2011 10:36 am ] | ||||||
Post subject: | How to Bucket Sort in Turing? | ||||||
What is it you are trying to achieve? <I'm supposed to bucket sort in Turing but I'm having difficulty. The program should allow to user the enter in the amount of numbers they want to sort and then randomize that many numbers. These numbers should then be sorted using Bucket Sort. > What is the problem you are having? <From what I understand, in bucket sort, you take the list to be sorted and you put them in different buckets. Numbers between a certain number range of say 1-10 go in bucket, numbers in the range of 11-20 go in another bucket, etc. I can't figure out how to create arrays for each new bucket required because I don't know how many number ranges there will be when the program randomizes the numbers. Also, my bubble sort algorithm isn't working and I think in Bucket Sort, you put the list of numbers into different buckets and then bubble sort within each bucket? I'm really stuck ![]() Describe what you have tried to solve this problem I can't think of anything, I've throughly searched the net for any solutions, and I finally just came here. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <Here's what I have so far, no luck though :/ >
Edit : Here's the new version, I fixed the bubble sort, but it's still not bucket sort :/. I still need to find out how to put everything in each bucket, then I can bubble sort, and then I need to find out how to put it all back together in the end.
Edit : I think I've assigned each number range to each bucket but I'm not sure how to apply bubble sort to each one and how to merge them together at the end.
Please specify what version of Turing you are using <4.1.1> |
Author: | Aange10 [ Sat Nov 26, 2011 12:26 pm ] | ||||
Post subject: | RE:How to Bucket Sort in Turing? | ||||
http://en.wikipedia.org/wiki/Bucket_sort Has lots of information, a step by step list, and pseudo code of how to do this. Quote: I can't figure out how to create arrays for each new bucket required because I don't know how many number ranges there will be when the program randomizes the number But you do know how many number ranges there are. There are n number ranges. So lets make that into buckets
Now to organize our buckets, we will just have a few for loops, and an initialization.
The for loops are the only really tricky parts. As for organizing, I'd look up bubble organizing on wikipedia. My friend taught be this a little while back, so I'll share with you what he gave me to learn bubble sorting. It's attached at the bottom |
Author: | Raknarg [ Sat Nov 26, 2011 1:54 pm ] | ||
Post subject: | RE:How to Bucket Sort in Turing? | ||
The key to this problem is using flexible arrays. A flexible array is the same as a regular array, but you can resize the upper bound. You resize like this:
That'll change the upper bound to 6. You can use this for unknown array bounds. |
Author: | Tony [ Sat Nov 26, 2011 2:03 pm ] | ||||||||
Post subject: | RE:How to Bucket Sort in Turing? | ||||||||
instead of
you could have
which will generalize the two cases. Unless you need to do something special with has_extra, but edge cases are typically discouraged.
This doesn't work for any case above 100 elements, and works poorly for cases less than 100. Remember that index can be evaluated at runtime
|
Author: | Aange10 [ Sat Nov 26, 2011 2:08 pm ] | ||||
Post subject: | Re: RE:How to Bucket Sort in Turing? | ||||
Tony @ 26/11/2011, 1:03 pm wrote: instead of
you could have
which will generalize the two cases. Unless you need to do something special with has_extra, but edge cases are typically discouraged. Wouldn't n/10 (lets say n = 101) wouldn't that then give me 10.1. Which you can't have an array defined as 1 .. 10.1? The extra bucket really wouldn't be 'extra' at all. It's just easier naming conventions Edit: And what is an edge case? |
Author: | Tony [ Sat Nov 26, 2011 2:13 pm ] | ||
Post subject: | RE:How to Bucket Sort in Turing? | ||
edit: edge case is a type of input that is treated in a special way, somehow different from general input. E.g., having "has_extra" flag for some but not all inputs. This introduces extra code paths and complexity. |
Author: | Aange10 [ Sat Nov 26, 2011 2:25 pm ] | ||
Post subject: | RE:How to Bucket Sort in Turing? | ||
Im not sure what == is >..<. But
is a syntax error. As for a special case, I try to keep it as irrelevant as i can; I try to keep it as similar to everything else, so there is less variety in what is happening. |
Author: | Tony [ Sat Nov 26, 2011 2:39 pm ] |
Post subject: | RE:How to Bucket Sort in Turing? |
reading time -- ceil |
Author: | Aange10 [ Sat Nov 26, 2011 2:47 pm ] |
Post subject: | RE:How to Bucket Sort in Turing? |
Amazing! Didn't know ceil was a procedure. Very cool, thanks Tony! |
Author: | turingmachine [ Sat Nov 26, 2011 2:52 pm ] |
Post subject: | RE:How to Bucket Sort in Turing? |
Thanks guys, especially Aange10, that code w/ documentation helped clear it up a lot ! Now I think I know how to out bubble sort each bucket and then put them together at the end .. thanks ![]() |
Author: | Aange10 [ Sat Nov 26, 2011 2:57 pm ] |
Post subject: | RE:How to Bucket Sort in Turing? |
Not a problem! Good luck |