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 |
<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 |
Sub sandman(sleepy() As ULong) |
||
Dim As |
Dim As Long lb = LBound(sleepy) |
||
Dim As |
Dim As Long ub = UBound(sleepy) |
||
Dim As |
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) + |
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 - |
count = count -1 |
||
End If |
End If |
||
Next |
Next |
||
Line 496: | Line 497: | ||
' ------=< MAIN >=------ |
' ------=< MAIN >=------ |
||
Dim As |
Dim As ULong i, arr(10) |
||
Dim As |
Dim As ULong lb = LBound(arr) |
||
Dim As |
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 |
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 |
<pre>unsorted 5 2 5 6 4 6 9 5 1 2 0 |
||
sorted |
sorted 0 1 2 2 4 5 5 5 6 6 9</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |