Next permutation in Pascal
up vote
-1
down vote
favorite
this is the assignment:
A permutation on N element set (of natural numbers {1, 2, ..., N} is any ordering of elements of this set. The underlying set has N! (mutually different) permutations. All permutations can be lexicographically ordered, i.e., in such the same way as the words in a dictionary are. Ordering of two particular permutations is given by the first element (from the beginning) where these permutations differ. Lexicographically first permutation is (1, 2, ..., N), the last one is (N, N-1, ..., 2, 1).
The first input line contains one positive integer N which is at most 100. The second line contains particular permutation on a set {1, 2, ..., N} while individual numbers are separated by spaces.
For a given permutation the program outputs directly the next permutation. If such a permutation does not exist, it outputs the word NEEXISTUJE
Example input:
6
1 3 6 2 5 4
Appropriate output:
1 3 6 4 2 5
And here is my meta-code of solution:
BEGIN
1) read length of permutation n
2) read permutation into the array Arr
3) Now we check if it's not the last permutation:
a) sum:=0
b) for i=1 to n do
begin
c) if Arr[i] = i then
d) sum:=sum+1
end
e) if sum=n then
f) writeln('NEEXISTUJE')
4) Else:
We search from end for two numbers i and i+1 following each other such as that:
Arr[i] < Arr[i+1]
//So in permutation 12 11 3 15 13 19 6 4 9 10 7 17 8 1 2 14 18 16 5 20; we would
find that i=19 fulfills the condition since Arr[19]=5<20=Arr[20] //
We remember i and value Arr[i], then we search from end again by comparing each array
value to value in i-th place until we find one that:
Arr[i] < Arr[j] where j is downward from n to i-1
//So in permutation 1 2 3 5 8 7 6 4; we would find i=4, j=7 //
We remember j and value Arr[j]. Now we write new array Arr2 length n:
a) for k=1 to i-1 do
begin
b) Arr2[k]:=Arr[k]
end
c) Arr2[i]:=Arr[j]
d) for k=0 to n-j-1
begin
e) Arr2[k+i+1]:=Arr[n-k]
end
f) Arr2[j]:=Arr[i]
g) for k=1 to j-i-1
begin
h) Arr2[k+j]:=Arr[j-k]
end
// So in general permutation we would get transformation
(a------b)(i)(c-----d)(j)(e---------f) ==>
(a------b)(j)(f-----e)(i)(d---------c) //
Write array Arr2 with spaces between indexes.
END
Haven't checked how well done is where we make the new array (I might be off by +1/-1 in one or more cases), however I'm confident this algorithm will work.
I'd appreciate any feedback/advices as how to actually write the code.
arrays algorithm permutation pascal
add a comment |
up vote
-1
down vote
favorite
this is the assignment:
A permutation on N element set (of natural numbers {1, 2, ..., N} is any ordering of elements of this set. The underlying set has N! (mutually different) permutations. All permutations can be lexicographically ordered, i.e., in such the same way as the words in a dictionary are. Ordering of two particular permutations is given by the first element (from the beginning) where these permutations differ. Lexicographically first permutation is (1, 2, ..., N), the last one is (N, N-1, ..., 2, 1).
The first input line contains one positive integer N which is at most 100. The second line contains particular permutation on a set {1, 2, ..., N} while individual numbers are separated by spaces.
For a given permutation the program outputs directly the next permutation. If such a permutation does not exist, it outputs the word NEEXISTUJE
Example input:
6
1 3 6 2 5 4
Appropriate output:
1 3 6 4 2 5
And here is my meta-code of solution:
BEGIN
1) read length of permutation n
2) read permutation into the array Arr
3) Now we check if it's not the last permutation:
a) sum:=0
b) for i=1 to n do
begin
c) if Arr[i] = i then
d) sum:=sum+1
end
e) if sum=n then
f) writeln('NEEXISTUJE')
4) Else:
We search from end for two numbers i and i+1 following each other such as that:
Arr[i] < Arr[i+1]
//So in permutation 12 11 3 15 13 19 6 4 9 10 7 17 8 1 2 14 18 16 5 20; we would
find that i=19 fulfills the condition since Arr[19]=5<20=Arr[20] //
We remember i and value Arr[i], then we search from end again by comparing each array
value to value in i-th place until we find one that:
Arr[i] < Arr[j] where j is downward from n to i-1
//So in permutation 1 2 3 5 8 7 6 4; we would find i=4, j=7 //
We remember j and value Arr[j]. Now we write new array Arr2 length n:
a) for k=1 to i-1 do
begin
b) Arr2[k]:=Arr[k]
end
c) Arr2[i]:=Arr[j]
d) for k=0 to n-j-1
begin
e) Arr2[k+i+1]:=Arr[n-k]
end
f) Arr2[j]:=Arr[i]
g) for k=1 to j-i-1
begin
h) Arr2[k+j]:=Arr[j-k]
end
// So in general permutation we would get transformation
(a------b)(i)(c-----d)(j)(e---------f) ==>
(a------b)(j)(f-----e)(i)(d---------c) //
Write array Arr2 with spaces between indexes.
END
Haven't checked how well done is where we make the new array (I might be off by +1/-1 in one or more cases), however I'm confident this algorithm will work.
I'd appreciate any feedback/advices as how to actually write the code.
arrays algorithm permutation pascal
4
You described stages of classicalnextpermutation
algorithm. What stops you from implementing it?
– MBo
Nov 8 at 11:27
2
Note - there is no need in separate checking whether current permutation is the lst one - if you have not found any inversion (your step 4), then stop with message.
– MBo
Nov 8 at 11:29
2
Possible duplicate of stackoverflow.com/questions/1622532/…. See also the Wikipedia page that gives a nice algorithm. Conversion to Pascal is left as an exercise.
– Jim Mischel
Nov 9 at 0:33
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
this is the assignment:
A permutation on N element set (of natural numbers {1, 2, ..., N} is any ordering of elements of this set. The underlying set has N! (mutually different) permutations. All permutations can be lexicographically ordered, i.e., in such the same way as the words in a dictionary are. Ordering of two particular permutations is given by the first element (from the beginning) where these permutations differ. Lexicographically first permutation is (1, 2, ..., N), the last one is (N, N-1, ..., 2, 1).
The first input line contains one positive integer N which is at most 100. The second line contains particular permutation on a set {1, 2, ..., N} while individual numbers are separated by spaces.
For a given permutation the program outputs directly the next permutation. If such a permutation does not exist, it outputs the word NEEXISTUJE
Example input:
6
1 3 6 2 5 4
Appropriate output:
1 3 6 4 2 5
And here is my meta-code of solution:
BEGIN
1) read length of permutation n
2) read permutation into the array Arr
3) Now we check if it's not the last permutation:
a) sum:=0
b) for i=1 to n do
begin
c) if Arr[i] = i then
d) sum:=sum+1
end
e) if sum=n then
f) writeln('NEEXISTUJE')
4) Else:
We search from end for two numbers i and i+1 following each other such as that:
Arr[i] < Arr[i+1]
//So in permutation 12 11 3 15 13 19 6 4 9 10 7 17 8 1 2 14 18 16 5 20; we would
find that i=19 fulfills the condition since Arr[19]=5<20=Arr[20] //
We remember i and value Arr[i], then we search from end again by comparing each array
value to value in i-th place until we find one that:
Arr[i] < Arr[j] where j is downward from n to i-1
//So in permutation 1 2 3 5 8 7 6 4; we would find i=4, j=7 //
We remember j and value Arr[j]. Now we write new array Arr2 length n:
a) for k=1 to i-1 do
begin
b) Arr2[k]:=Arr[k]
end
c) Arr2[i]:=Arr[j]
d) for k=0 to n-j-1
begin
e) Arr2[k+i+1]:=Arr[n-k]
end
f) Arr2[j]:=Arr[i]
g) for k=1 to j-i-1
begin
h) Arr2[k+j]:=Arr[j-k]
end
// So in general permutation we would get transformation
(a------b)(i)(c-----d)(j)(e---------f) ==>
(a------b)(j)(f-----e)(i)(d---------c) //
Write array Arr2 with spaces between indexes.
END
Haven't checked how well done is where we make the new array (I might be off by +1/-1 in one or more cases), however I'm confident this algorithm will work.
I'd appreciate any feedback/advices as how to actually write the code.
arrays algorithm permutation pascal
this is the assignment:
A permutation on N element set (of natural numbers {1, 2, ..., N} is any ordering of elements of this set. The underlying set has N! (mutually different) permutations. All permutations can be lexicographically ordered, i.e., in such the same way as the words in a dictionary are. Ordering of two particular permutations is given by the first element (from the beginning) where these permutations differ. Lexicographically first permutation is (1, 2, ..., N), the last one is (N, N-1, ..., 2, 1).
The first input line contains one positive integer N which is at most 100. The second line contains particular permutation on a set {1, 2, ..., N} while individual numbers are separated by spaces.
For a given permutation the program outputs directly the next permutation. If such a permutation does not exist, it outputs the word NEEXISTUJE
Example input:
6
1 3 6 2 5 4
Appropriate output:
1 3 6 4 2 5
And here is my meta-code of solution:
BEGIN
1) read length of permutation n
2) read permutation into the array Arr
3) Now we check if it's not the last permutation:
a) sum:=0
b) for i=1 to n do
begin
c) if Arr[i] = i then
d) sum:=sum+1
end
e) if sum=n then
f) writeln('NEEXISTUJE')
4) Else:
We search from end for two numbers i and i+1 following each other such as that:
Arr[i] < Arr[i+1]
//So in permutation 12 11 3 15 13 19 6 4 9 10 7 17 8 1 2 14 18 16 5 20; we would
find that i=19 fulfills the condition since Arr[19]=5<20=Arr[20] //
We remember i and value Arr[i], then we search from end again by comparing each array
value to value in i-th place until we find one that:
Arr[i] < Arr[j] where j is downward from n to i-1
//So in permutation 1 2 3 5 8 7 6 4; we would find i=4, j=7 //
We remember j and value Arr[j]. Now we write new array Arr2 length n:
a) for k=1 to i-1 do
begin
b) Arr2[k]:=Arr[k]
end
c) Arr2[i]:=Arr[j]
d) for k=0 to n-j-1
begin
e) Arr2[k+i+1]:=Arr[n-k]
end
f) Arr2[j]:=Arr[i]
g) for k=1 to j-i-1
begin
h) Arr2[k+j]:=Arr[j-k]
end
// So in general permutation we would get transformation
(a------b)(i)(c-----d)(j)(e---------f) ==>
(a------b)(j)(f-----e)(i)(d---------c) //
Write array Arr2 with spaces between indexes.
END
Haven't checked how well done is where we make the new array (I might be off by +1/-1 in one or more cases), however I'm confident this algorithm will work.
I'd appreciate any feedback/advices as how to actually write the code.
arrays algorithm permutation pascal
arrays algorithm permutation pascal
asked Nov 8 at 10:59
Jan Lhoták
1
1
4
You described stages of classicalnextpermutation
algorithm. What stops you from implementing it?
– MBo
Nov 8 at 11:27
2
Note - there is no need in separate checking whether current permutation is the lst one - if you have not found any inversion (your step 4), then stop with message.
– MBo
Nov 8 at 11:29
2
Possible duplicate of stackoverflow.com/questions/1622532/…. See also the Wikipedia page that gives a nice algorithm. Conversion to Pascal is left as an exercise.
– Jim Mischel
Nov 9 at 0:33
add a comment |
4
You described stages of classicalnextpermutation
algorithm. What stops you from implementing it?
– MBo
Nov 8 at 11:27
2
Note - there is no need in separate checking whether current permutation is the lst one - if you have not found any inversion (your step 4), then stop with message.
– MBo
Nov 8 at 11:29
2
Possible duplicate of stackoverflow.com/questions/1622532/…. See also the Wikipedia page that gives a nice algorithm. Conversion to Pascal is left as an exercise.
– Jim Mischel
Nov 9 at 0:33
4
4
You described stages of classical
nextpermutation
algorithm. What stops you from implementing it?– MBo
Nov 8 at 11:27
You described stages of classical
nextpermutation
algorithm. What stops you from implementing it?– MBo
Nov 8 at 11:27
2
2
Note - there is no need in separate checking whether current permutation is the lst one - if you have not found any inversion (your step 4), then stop with message.
– MBo
Nov 8 at 11:29
Note - there is no need in separate checking whether current permutation is the lst one - if you have not found any inversion (your step 4), then stop with message.
– MBo
Nov 8 at 11:29
2
2
Possible duplicate of stackoverflow.com/questions/1622532/…. See also the Wikipedia page that gives a nice algorithm. Conversion to Pascal is left as an exercise.
– Jim Mischel
Nov 9 at 0:33
Possible duplicate of stackoverflow.com/questions/1622532/…. See also the Wikipedia page that gives a nice algorithm. Conversion to Pascal is left as an exercise.
– Jim Mischel
Nov 9 at 0:33
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206356%2fnext-permutation-in-pascal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
You described stages of classical
nextpermutation
algorithm. What stops you from implementing it?– MBo
Nov 8 at 11:27
2
Note - there is no need in separate checking whether current permutation is the lst one - if you have not found any inversion (your step 4), then stop with message.
– MBo
Nov 8 at 11:29
2
Possible duplicate of stackoverflow.com/questions/1622532/…. See also the Wikipedia page that gives a nice algorithm. Conversion to Pascal is left as an exercise.
– Jim Mischel
Nov 9 at 0:33