1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| function print_arr(dataArr, length) local str = ""; for i = 1, length do str = str.."\t"..tostring(dataArr[i]); end print(str) end
function swap(data, i, index) t = data[index]; data[index] = data[i]; data[i] = t; end
function permutation(data, sbegin, send) if sbegin == send then print_arr(data, 4); return; end
for i = sbegin, send do swap(data, sbegin, i); permutation(data, sbegin + 1, send); swap(data, sbegin, i); end end
data = {1, 2, 3, 4}; permutation(data, 1, 4);
|