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.










share|improve this question


















  • 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















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.










share|improve this question


















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 10:59









Jan Lhoták

1




1








  • 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














  • 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








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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff