-
-
Notifications
You must be signed in to change notification settings - Fork 342
Add Sleep Sort Algorithm in R #240
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||
| # Sleep sort in R: | ||||||||
|
|
||||||||
| # SleepSort is a (mostly humorous) sorting algorithm that launches one task per | ||||||||
| # element, sleeps proportional to the element's value, then outputs the value | ||||||||
| # upon waking. The values are observed in order of their completion times. | ||||||||
| # | ||||||||
| # This implementation targets Windows compatibility by using a PSOCK cluster | ||||||||
| # from the 'parallel' package and leveraging file modification times to capture | ||||||||
| # the order in which tasks finish. | ||||||||
| # | ||||||||
| # Notes: | ||||||||
| # - Works for numeric vectors. Negative values are supported by offsetting so | ||||||||
| # all sleep times are non-negative (relative to the minimum value). | ||||||||
| # - Large values can cause long sleep times; use 'scale' to control delay. | ||||||||
| # - This is for educational/demonstration purposes and not recommended for | ||||||||
| # production sorting. | ||||||||
|
|
||||||||
| sleep.sort <- function(elements.vec, scale = 0.01, max.workers = NULL) { | ||||||||
| # Handle trivial cases | ||||||||
| n <- length(elements.vec) | ||||||||
| if (n <= 1) return(elements.vec) | ||||||||
| if (!is.numeric(elements.vec)) stop("sleep.sort: elements.vec must be numeric") | ||||||||
|
|
||||||||
| # Shift to make non-negative delays and avoid negative sleep | ||||||||
| min.val <- min(elements.vec) | ||||||||
| offset <- if (min.val < 0) -min.val else 0 | ||||||||
|
|
||||||||
| # Cap scale to a reasonable positive number | ||||||||
| if (!is.numeric(scale) || scale <= 0) stop("sleep.sort: 'scale' must be positive numeric") | ||||||||
|
|
||||||||
| # Determine number of workers | ||||||||
| if (is.null(max.workers)) { | ||||||||
| # Conservative default | ||||||||
| max.workers <- max(1L, min(4L, n)) | ||||||||
| } | ||||||||
|
|
||||||||
| # Prepare an output directory to record completion times | ||||||||
| outdir <- file.path(tempdir(), paste0("sleep_sort_", as.integer(runif(1) * 1e9))) | ||||||||
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) | ||||||||
|
||||||||
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) | |
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) | |
| on.exit(unlink(outdir, recursive = TRUE), add = TRUE) |
siriak marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Oct 23, 2025
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.
The magic number 50 for retry attempts lacks context. Extract this as a named constant (e.g., max_retries <- 50) to clarify its purpose and make it easier to adjust.
| while (tries < 50 && length(files) != n) { | |
| max_retries <- 50 | |
| while (tries < max_retries && length(files) != n) { |
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.
The random directory name generation could produce collisions if multiple instances run simultaneously. Use
tempfile()instead, which guarantees unique temporary paths:outdir <- tempfile(pattern = 'sleep_sort_').