Sorting algorithms/Radix sort: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
imported>Thebeez (Added uBasic/4tH version) |
||
Line 4,139: | Line 4,139: | ||
</pre> |
</pre> |
||
=={{header|uBasic/4tH}}== |
|||
{{Trans|BBC BASIC}} |
|||
In uBasic/4tH you can't pass an array as a parameter. All arrays are global. |
|||
<syntaxhighlight lang="qbasic">Dim @t(10) |
|||
Push 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
|||
For i = 0 Step 1 While Used() |
|||
@t(i) = Pop() |
|||
Next |
|||
Proc _Radixsort(10, 10) |
|||
For i = 0 TO 9 |
|||
Print @t(i), |
|||
Next |
|||
Print |
|||
End |
|||
_Radixsort |
|||
Param (2) |
|||
Local (5) |
|||
Dim @b(a@) |
|||
Dim @u(b@) |
|||
For e@ = 0 TO a@-1 |
|||
If @t(e@) < f@ Then f@ = @t(e@) |
|||
If @t(e@) > g@ Then g@ = @t(e@) |
|||
Next |
|||
For e@ = 0 To a@-1 : @t(e@) = @t(e@) - f@ : Next |
|||
g@ = g@ - f@ |
|||
d@ = 1 |
|||
Do While g@ / d@ |
|||
For e@ = 0 to a@-1 : @u(e@) = 0 : Next |
|||
For e@ = 0 TO a@-1 |
|||
@u(@t(e@) / d@ % b@) = @u(@t(e@) / d@ % b@) + 1 |
|||
Next |
|||
For e@ = 1 TO b@-1 |
|||
@u(e@) = @u(e@) + @u(e@ - 1) |
|||
Next |
|||
For e@ = a@-1 TO 0 Step -1 |
|||
c@ = @t(e@) / d@ % b@ |
|||
@u(c@) = @u(c@)-1 |
|||
@b(@u(c@)) = @t(e@) |
|||
Next |
|||
For e@ = 0 To a@-1 : @t(e@) = @b(e@) : Next |
|||
d@ = d@ * b@ |
|||
Loop |
|||
For e@ = 0 To a@-1 : @t(e@) = @t(e@) + f@ : Next |
|||
Return</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>-31 0 1 2 2 4 65 83 99 782 |
|||
0 OK, 0:177</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
This is based on the approach used [https://www.geeksforgeeks.org/radix-sort/ here] which I've adjusted to deal with negative elements. |
This is based on the approach used [https://www.geeksforgeeks.org/radix-sort/ here] which I've adjusted to deal with negative elements. |