-
-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat]: Added Sleep Sort Implementation #188
base: master
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,39 @@ | |||
import { sleepSort } from "../sleep_sort"; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the tests in a describe("Sleep sort", ...)
. Fine otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with the describe. @appgurueu
@@ -35,7 +35,7 @@ export const sleepSort = async (arr: number[]): Promise<number[]> => { | |||
|
|||
await Promise.all( | |||
arr.map(async (n: number) => { | |||
await sleep(n); | |||
await sleep(n*100); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the seemingly arbitrary *100
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So setting sleep for just n milliseconds would sometimes not work for some cases as it supposed to.
But due to low sleep time (n milliseconds), the first value would get resolved first before the iteration to reach the other end of an array where lower values might be present.
Let's say the array to be sorted is [1,3,5,6,0] which should result in [0,1,3,5,6].
In this case, 1 (index 0) will gets resolved first ( ~ 1ms) before reaching the value 0 ( last index).
Putting a multiplier gave correct results. Not sure though its the correct way of dealing with this .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there probably is no guarantee made regarding wake-up order, so sleeping is probably technically an inherently flawed way to sort an array; one could always construe a scenario where it doesn't work.
A hack of improving the likelihood of correctness like this should at the very least be explained; the "algorithm" would need at least a big fat warning that it can be arbitrarily incorrect, which IMO also makes me question whether we should add it, given that it can't guarantee reliability...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there probably is no guarantee made regarding wake-up order, so sleeping is probably technically an inherently flawed way to sort an array; one could always construe a scenario where it doesn't work.
Agreed. Algorithm is flawed and not relevant to any actual scenario. A bit go-through resources and couldn't find its
practical usage. (https://stackoverflow.com/questions/74917807/is-there-any-practical-use-case-for-sleep-sort)
One walkaround could be sleeping the number for (n^2) time which would avoid the edge case for very small numbers few distance apart but will get irrelevant for bigger numbers unless we can come up with something like dynamically handling sleep time according to the size of numbers. But I guess its not worth the effort.
A hack of improving the likelihood of correctness like this should at the very least be explained; the "algorithm" would need at least a big fat warning that it can be arbitrarily incorrect, which IMO also makes me question whether we should add it, given that it can't guarantee reliability...
You are right. Its tough to prove that algorithm is reliable and can't guarantee will work in every scenario.
Its impractical but indeed an interesting concept ! which makes me think people should be aware of it.
But for that like you said, a warning could be made about its reliability and practicality and its just an another fun & creative way to sort an array.
No description provided.