Sorting algorithms/Bogosort: Difference between revisions
Content added Content deleted
(Add Factor) |
|||
Line 104: | Line 104: | ||
3 6 7 9 |
3 6 7 9 |
||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{trans|python}} |
|||
{{works with|ALGOL 68|Standard - no extensions to language used}} |
|||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
|||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
|||
<lang algol>MODE TYPE = INT; |
|||
PROC random shuffle = (REF[]TYPE l)VOID: ( |
|||
INT range = UPB l - LWB l + 1; |
|||
FOR index FROM LWB l TO UPB l DO |
|||
TYPE tmp := l[index]; |
|||
INT other := ENTIER (LWB l + random * range); |
|||
l[index] := l[other]; |
|||
l[other] := tmp |
|||
OD |
|||
); |
|||
PROC in order = (REF[]TYPE l)BOOL: ( |
|||
IF LWB l >= UPB l THEN |
|||
TRUE |
|||
ELSE |
|||
TYPE last := l[LWB l]; |
|||
FOR index FROM LWB l + 1 TO UPB l DO |
|||
IF l[index] < last THEN |
|||
GO TO return false |
|||
FI; |
|||
last := l[index] |
|||
OD; |
|||
TRUE EXIT |
|||
return false: FALSE |
|||
FI |
|||
); |
|||
PROC bogo sort = (REF[]TYPE l)REF[]TYPE: ( |
|||
WHILE NOT in order(l) DO |
|||
random shuffle(l) |
|||
OD; |
|||
l |
|||
); |
|||
[6]TYPE sample := (61, 52, 63, 94, 46, 18); |
|||
print((bogo sort(sample), new line)) |
|||
</lang> |
|||
Output: |
|||
+18 +46 +52 +61 +63 +94 |
|||
=={{header|C}}== |
=={{header|C}}== |
||
<lang c>#include <stdio.h> |
<lang c>#include <stdio.h> |