I am trying to understand a permutation algorithm given here what I am not clear is with first block of pseudo code where they mention
array = [1, 2, 3, 4]
function permutation(start, end):
#i will go from start to end
for i -> (start, end+1):
permutation(start+1,end)
why has end+1 used in for i loop this is not clear to me as far as I understand end+1 should go out of index of array to which permutation has to be applied, but this is not the case here this is what I am not clear with.
The author is familiar in Python, and uses the same (unfortunate) idiom in the pseudo code. In Python, the start of a range is inclusive, while the end is exclusive. Later on that page the Python code excerpt proves that that indeed is the case:
for i in range(start, end+1):
With this code i
will be sequentially assigned all integers from start
to end
inclusive, but excluding the end + 1
.
In C one would often use <
in loops - then it would happen there too:
for (size_t i = start; start < end + 1; start++)
^^^^^^^
though more natural would be to write
for (size_t i = start; start <= end; start++)