Sorting algorithms/Comb sort: Difference between revisions
Content added Content deleted
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
(Added XPL0 example.) |
||
Line 3,589: | Line 3,589: | ||
After : [1, 2, 2, 3, 4, 5, 6, 6, 7] |
After : [1, 2, 2, 3, 4, 5, 6, 6, 7] |
||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
|||
{{trans|ALGOL W}} |
|||
<syntaxhighlight lang "XPL0"> |
|||
\Comb sorts in-place the array of integers Input with bounds LB :: UB |
|||
procedure CombSort ( Input, LB, UB ); |
|||
integer Input, LB, UB; |
|||
integer InputSize, Gap, I, Swapped, T, IGap; |
|||
begin |
|||
InputSize := ( UB - LB ) + 1; |
|||
if InputSize > 1 then begin |
|||
\more than one element, so must sort |
|||
Gap := InputSize; \initial Gap is the whole array size |
|||
Swapped := true; |
|||
while Gap # 1 or Swapped do begin |
|||
\update the Gap value for a next comb |
|||
Gap := fix( Floor(float(Gap) / 1.25) ); |
|||
if Gap < 1 then begin |
|||
\ensure the Gap is at least 1 |
|||
Gap := 1 |
|||
end; \if_Gap_lt_1 |
|||
Swapped := false; |
|||
\a single "comb" over the input list |
|||
I := LB; |
|||
while I + Gap <= UB do begin |
|||
T := Input( I ); |
|||
IGap := I + Gap; |
|||
if T > Input( IGap ) then begin |
|||
\need to swap out-of-order items |
|||
Input( I ) := Input( IGap ); |
|||
Input( IGap ) := T; |
|||
\Flag a swap has occurred, so the list is not guaranteed sorted yet |
|||
Swapped := true |
|||
end; \if_t_gt_input__iGap |
|||
I := I + 1 |
|||
end \while_I_plus_Gap_le_UB |
|||
end \while_Gap_ne_1_or_swapped |
|||
end \if_inputSize_gt_1 |
|||
end; \combSort |
|||
integer Data, I; |
|||
begin \test |
|||
Data:= [0, 9, -4, 0, 2, 3, 77, 1]; |
|||
for I := 1 to 7 do begin Text(0, " "); IntOut(0, Data( I ) ) end; |
|||
CombSort( Data, 1, 7 ); |
|||
Text(0, ( " -> " ) ); |
|||
for I := 1 to 7 do begin Text(0, " "); IntOut(0, Data( I ) ) end; |
|||
end</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
9 -4 0 2 3 77 1 -> -4 0 1 2 3 9 77</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |