Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ package-lock.json

# F#
Program.fs

# scala
.metals
.scala-build
48 changes: 48 additions & 0 deletions archive/s/scala/SleepSort.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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
}
}