R icon indicating copy to clipboard operation
R copied to clipboard

Add Sleep Sort Algorithm in R

Open Siddhram opened this issue 2 months ago • 2 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

  • 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

Siddhram avatar Oct 18 '25 09:10 Siddhram