Skip to content

Conversation

@Siddhram
Copy link
Contributor

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

  • Algorithm Type: Demonstrative / Novelty sorting algorithm
  • Target Data: Numeric vectors (supports negative values via offsetting)
  • Execution: Uses parallel tasks via a PSOCK cluster from the parallel package
  • Windows Compatible: Handles timing via file modification times to capture completion order
  • Notes: Intended for educational purposes; not recommended for production due to its reliance on sleep delays

Features

  • Sorts numeric vectors using timed task execution
  • Supports negative numbers by shifting all values to non-negative sleep times
  • Adjustable scale parameter to control sleep duration
  • Parallel execution with configurable number of workers
  • Automatic fallback to base R sort() if parallel execution fails

Complexity

  • Time Complexity (TC): O(n) in terms of task scheduling, but overall runtime depends on the largest element × scale
  • Space Complexity (SC): O(n) for temporary files and parallel tasks

@siriak siriak requested a review from Copilot October 23, 2025 19:45
Copy link

Copilot AI left a 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)))
Copy link

Copilot AI Oct 23, 2025

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_').

Suggested change
outdir <- file.path(tempdir(), paste0("sleep_sort_", as.integer(runif(1) * 1e9)))
outdir <- tempfile(pattern = "sleep_sort_")

Copilot uses AI. Check for mistakes.

# 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)
Copy link

Copilot AI Oct 23, 2025

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.

Suggested change
dir.create(outdir, recursive = TRUE, showWarnings = FALSE)
dir.create(outdir, recursive = TRUE, showWarnings = FALSE)
on.exit(unlink(outdir, recursive = TRUE), add = TRUE)

Copilot uses AI. Check for mistakes.
if (length(files) != n) {
# In rare cases, the filesystem may lag; simple retry loop
tries <- 0
while (tries < 50 && length(files) != n) {
Copy link

Copilot AI Oct 23, 2025

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.

Suggested change
while (tries < 50 && length(files) != n) {
max_retries <- 50
while (tries < max_retries && length(files) != n) {

Copilot uses AI. Check for mistakes.
@siriak
Copy link
Member

siriak commented Oct 23, 2025

Please address comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants