I think you could take the sort values, use them as memory addresses (or array offsets), and write the original index of each value into its corresponding pointer, then go back and iterate through that entire chunk of memory to find each of them. That might technically be linear time? Really N + M, where N is the number of values and M is their full range. It would have hilariously inefficient usage of memory and a pretty low cap on the range of possible sort values, but still.
Spaghetti Sort (2018)
31–40 of 55 posts
Re: Spaghetti Sort (2018)
#32I was thinking about how this (and other "physical sorts" mentioned by commenters) sorts instantly by using a physical property of the members, and I began to consider what the computer equivalent would be of "physical sorting". I think you could take the sort values, use them as memory addresses (or array offsets), and write the original index of each value into its corresponding pointer, then go back and iterate th…
Re: Spaghetti Sort (2018)
#33Linear time O(n) means that as you keep adding numbers to sort, the time taken is bounded by a linear function of how many numbers you have. Since there is an upper bound on the number of spaghetti strands you can hold in your hand, eventually you need to "Slam their lower sides on the table" in batches, and then merge sort the results. However because unlike in merge sort there is an upper bound on the size of the s…
You can replace the hand with any device that holds the spaghetti. This really is an O(n) algorithm. Spaghetti sort seems to be an analogue variant of radix sort.
0: Manufacture a pair of hands big enough to hold all the spaghetti you'll need in the later steps.
Does this step take linear time as well?
Re: Spaghetti Sort (2018)
#34Re: Spaghetti Sort (2018)
#35Re: Spaghetti Sort (2018)
#36I was thinking about how this (and other "physical sorts" mentioned by commenters) sorts instantly by using a physical property of the members, and I began to consider what the computer equivalent would be of "physical sorting". I think you could take the sort values, use them as memory addresses (or array offsets), and write the original index of each value into its corresponding pointer, then go back and iterate th…
I think you can also use a large bitmap if all you want is sorted values.
BTW, Bloom filters work on a quite similar idea, and they come in handy in database setups.
Re: Spaghetti Sort (2018)
#37Has someone calculated at what n I will sort faster than my PC?
Pretty big. You're comparing quicksort at O(n log n) to hand at O(n). A computer is about a billion times faster per operation, so log n = 1 billion, or n = 2^billion.
Re: Spaghetti Sort (2018)
#38This reminds me of my favorite joke algorithm, sleepsort[0]. A trivial implementation looks like this: #!/bin/bash for int in $@; do # input must be a list of positive integers (sleep $int; echo $int) & done; wait I'm not quite sure how to describe it in terms of big O notation. [0] https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort
Re: Spaghetti Sort (2018)
#39Imagine the graph is drawn on an handkerchief. Pick any vertex of the graph and let the handkerchief "fall" around it.
Now, with your other hand, pick the point which is the lowest (farthest away from the one in your hand). The handkerchief falls again around that new point.
Again, with your other hand pick the point which is the lowest. The two points in your hands are those farthest away.
(didn't find the reference of this algo, so it may be wrong; just correct me if it is)
Re: Spaghetti Sort (2018)
#40This reminds me of my favorite joke algorithm, sleepsort[0]. A trivial implementation looks like this: #!/bin/bash for int in $@; do # input must be a list of positive integers (sleep $int; echo $int) & done; wait I'm not quite sure how to describe it in terms of big O notation. [0] https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort