Sorting algorithms/Comb sort: Difference between revisions

Content added Content deleted
Line 225: Line 225:
declare swaps bit (1) aligned;
declare swaps bit (1) aligned;


gap = hbound(A,1); /* initialize the gap size. */
gap = hbound(A,1) - lbound(A,1); /* initialize the gap size. */
do until (gap <= 1 & swaps);
do until (gap <= 1 & swaps);
/* update the gap value for a next comb. */
/* update the gap value for a next comb. */
gap = gap / 1.25;
put skip data (gap);
gap = gap / 1.25e0;
put skip data (gap);
swaps = '1'b;
swaps = '1'b;
/* a single "comb" over the array. */
/* a single "comb" over the array. */
do i = 0 by 1 until (i + gap >= hbound(A,1));
do i = lbound(A,1) by 1 until (i + gap >= hbound(A,1));
if A(i) > A(i+gap) then
if A(i) > A(i+gap) then
do;
do;
t = A(i); A(i) = A(i+gap); A(i+gap) = t;
t = A(i); A(i) = A(i+gap); A(i+gap) = t;
swaps := '0'b; /* Flag a swap has occurred, so */
swaps = '0'b; /* Flag a swap has occurred, so */
/* the list is not guaranteed sorted. */
/* the list is not guaranteed sorted. */
end;
end;
end;
end;