-
-
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?
Conversation
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.
Pull Request Overview
This PR adds a Sleep Sort implementation to the repository, a novelty sorting algorithm that sorts elements by launching parallel tasks that sleep for durations proportional to each element's value. The algorithm uses R's parallel package with PSOCK clusters and file modification times to capture completion order.
Key Changes:
- Implements sleep sort with support for negative numbers via offsetting
- Uses parallel execution with configurable workers and adjustable sleep scaling
- Includes fallback to base
sort()if parallel execution fails
| } | ||
|
|
||
| # Prepare an output directory to record completion times | ||
| outdir <- file.path(tempdir(), paste0("sleep_sort_", as.integer(runif(1) * 1e9))) |
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 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_').
| outdir <- file.path(tempdir(), paste0("sleep_sort_", as.integer(runif(1) * 1e9))) | |
| outdir <- tempfile(pattern = "sleep_sort_") |
|
|
||
| # 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) |
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 temporary directory is never cleaned up after function execution. Add on.exit(unlink(outdir, recursive = TRUE), add = TRUE) after line 39 to ensure cleanup even if an error occurs.
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) | |
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) | |
| on.exit(unlink(outdir, recursive = TRUE), add = TRUE) |
| if (length(files) != n) { | ||
| # In rare cases, the filesystem may lag; simple retry loop | ||
| tries <- 0 | ||
| while (tries < 50 && length(files) != n) { |
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) { |
|
Please address comments |
This PR introduces Sleep Sort, a playful and educational sorting algorithm implemented in R.
Sleep Sort launches one task per element, sleeps for a duration proportional to the element's value, and outputs the value when the task completes. The values appear in ascending order based on their "wake-up" times.
Overview
parallelpackageFeatures
scaleparameter to control sleep durationsort()if parallel execution failsComplexity
scale