Sleep Sort in Scala

Published on 06 April 2025 (Updated: 06 April 2025)

Welcome to the Sleep Sort in Scala page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

import scala.collection.mutable.ListBuffer
import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.concurrent.duration._

object SleepSort {
  def main(args: Array[String]): Unit = {
    var result = invalidChecker(args)
      
    if(!result){ // After going through checker, it will output result to procede with SleepSort or not
      // println(result)
      println("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"")
    } else {
      val numbers = args.flatMap(_.split(",")).map(_.trim).filter(_.nonEmpty).map(_.toInt)
      println(sleepSort(numbers))
    }
  }
    

  // Checking for formating, empty array, and Non-numeric values
  def invalidChecker(args: Array[String]): Boolean = args match {
    case null | Array() => false  // "No Input"
    case arr if arr.forall(_.isEmpty) =>  false //"Empty Input"
    case arr if arr.forall(_.length == 1) && arr.length == 1 => false //"Invalid Input: Not A List"
    case arr if !arr.exists(_.contains(",")) => false //"Invalid Input: Wrong Format"
    case _ =>  true
  }

  // delaying time to add a value to list base on it weight
  def sleepSort(args: Array[Int]): String = {
    val delayTimer: Long = 100L
    val output = new ListBuffer[Int]()

    val futures = args.map { num =>
      Future {
        blocking {
          Thread.sleep(num * delayTimer) // Delay execution
        }
        output.synchronized {
          output += num
        }
      }
    }.toList // Convert Array to List to fix type mismatch

    Await.result(Future.sequence(futures), Duration.Inf) // Wait for all futures
    output.mkString(", ") // Format output correctly
  }
}

Sleep Sort in Scala was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.