Jump to content

Sorting algorithms/Sleep sort: Difference between revisions

added FreeBasic
(Omit Axe)
(added FreeBasic)
Line 432:
end if</lang>
 
=={{header|FreeBASIC}}==
Can't use FreeBASIC '''sleep''' since it halts the program.
Instead it uses a second array filled with times based on the value of number, this array is check against the timer. If the timer is past the stored time the value is printed.
<lang FreeBASIC>' version 05-07-2015
' compile with: fbc -s console
' compile with: fbc -s console -exx (for bondry check on the array's)
' not very well suited for large numbers and large array's
 
Sub sandman(sleepy() As UInteger)
Dim As Integer lb = LBound(sleepy)
Dim As Integer ub = UBound(sleepy)
Dim As Integer i, count = ub
Dim As Double wakeup(lb To ub)
Dim As Double t = Timer
 
For i = lb To ub
wakeup(i) = sleepy(i) + 1 + t
Next
 
Do
t = Timer
For i = lb To ub
If wakeup(i) <= t Then
Print Using "####";sleepy(i);
wakeup(i) = 1e9 ' mark it as used
count = count - 1
End If
Next
Sleep (1 - (Timer - t)) * 300, 1 ' reduce CPU load
Loop Until count < lb
 
End Sub
 
' ------=< MAIN >=------
 
Dim As UInteger i, arr(10)
Dim As UInteger lb = LBound(arr)
Dim As UInteger ub = UBound(arr)
 
Randomize
For i = lb To ub
arr(i) = Int(Rnd*10)+1
Next
 
Print "unsorted ";
For i = lb To ub
Print Using "####";arr(i);
Next
Print : Print
 
Print " sorted ";
sandman(arr())
 
Print : Print
 
' empty keyboard buffer
While Inkey <> "" : Var _key_ = Inkey : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>unsorted 1 1 2 1 9 1 4 4 4 7 2
 
sorted 1 1 1 1 2 2 4 4 4 7 9</pre>
=={{header|Go}}==
<lang go>package main
457

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.