Sorting algorithms/Sleep sort: Difference between revisions

Content added Content deleted
m (→‎{{header|FreeBASIC}}: removed redundant code)
m (→‎{{header|FreeBASIC}}: minor change)
Line 464: Line 464:
Can't use FreeBASIC '''sleep''' since it halts the program.
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.
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
<lang freebasic>' version 21-10-2016
' compile with: fbc -s console
' compile with: fbc -s console
' compile with: fbc -s console -exx (for bondry check on the array's)
' compile with: fbc -s console -exx (for bondry check on the array's)
' not very well suited for large numbers and large array's
' not very well suited for large numbers and large array's
' positive numbers only


Sub sandman(sleepy() As UInteger)
Sub sandman(sleepy() As ULong)
Dim As Integer lb = LBound(sleepy)
Dim As Long lb = LBound(sleepy)
Dim As Integer ub = UBound(sleepy)
Dim As Long ub = UBound(sleepy)
Dim As Integer i, count = ub
Dim As Long i, count = ub
Dim As Double wakeup(lb To ub)
Dim As Double wakeup(lb To ub)
Dim As Double t = Timer
Dim As Double t = Timer


For i = lb To ub
For i = lb To ub
wakeup(i) = sleepy(i) + 1 + t
wakeup(i) = sleepy(i) +1 + t
Next
Next


Line 486: Line 487:
Print Using "####";sleepy(i);
Print Using "####";sleepy(i);
wakeup(i) = 1e9 ' mark it as used
wakeup(i) = 1e9 ' mark it as used
count = count - 1
count = count -1
End If
End If
Next
Next
Line 496: Line 497:
' ------=< MAIN >=------
' ------=< MAIN >=------


Dim As UInteger i, arr(10)
Dim As ULong i, arr(10)
Dim As UInteger lb = LBound(arr)
Dim As ULong lb = LBound(arr)
Dim As UInteger ub = UBound(arr)
Dim As ULong ub = UBound(arr)


Randomize
Randomize Timer
For i = lb To ub
For i = lb To ub -1 ' leave last one zero
arr(i) = Int(Rnd*10)+1
arr(i) = Int(Rnd * 10) +1
Next
Next


Line 517: Line 518:


' empty keyboard buffer
' empty keyboard buffer
While Inkey <> "" : Wend
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</lang>
{{out}}
{{out}}
<pre>unsorted 1 1 2 1 9 1 4 4 4 7 2
<pre>unsorted 5 2 5 6 4 6 9 5 1 2 0


sorted 1 1 1 1 2 2 4 4 4 7 9</pre>
sorted 0 1 2 2 4 5 5 5 6 6 9</pre>


=={{header|Go}}==
=={{header|Go}}==