Algorithm 154: combination in lexicographical order
Charles J. Mifsud · Communications of the ACM · 1963
procedure COMB1 (n,r,I); integer n, r; integer array I;commentThe distinct combinations of the first n integers taken r at a time are generated in I in lexicographical order starting with an initial combination of the r integers 1, 2, ... , r.Each call of the procedure, after the first, must have in I the previous generated combination.The Boolean variable first is nonlocal to COMB1 and must be true before the first call.Thereafter first remains false until all combinations have been generated.When calling COMB1 with I containing n --r + 1, n --r + 2, • .-,n, I is left unchanged and first is set true; begin integer 8, j; if first then begin for ] := 1 step 1 until r do l[i] := i; firs t := false; go to EXIT end; begin ifl[r] < n then begin I[r] := I[r] + 1; go to EXIT end ; for j := r step --1 until 2 do if I[]--1] < n --r + j --1 then begin I[]--1] := I[i--1] + 1; for s := j step I until r do 118] := I[j--1] + s --(j--l); go to EXIT end end; first := true;