Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Permutations of elements
07-26-2009, 08:48 PM
Post: #1
Permutations of elements
This is a bit advanced algorithm for permutations of elements. It writes in file elements which you specified ! It uses recursive algorithm :
Code:
program permutations_of_elements;
uses wincrt;
var
  a:array[1..100] of integer;
  i,n:integer;
  s:array[1..100] of char;
  out:text;
  location:string;
procedure output;
var
  i:integer;
begin
for i:=1 to n do
  write(out,s[a[i]]);
writeln(out);
end;

function check(k:integer):boolean;
var
  i:integer;
  b:boolean;
begin
  b:=false;
  for i:=1 to k-1 do
    if b=false then
      if a[i]=a[k] then
        b:=true;
if b=true then
   check:=false
else
   check:=true;
end;
    
procedure recursion(index:integer);
var
   i:integer;
begin
  if index>n then
    output
  else
    begin
     for i:=1 to n do
      begin
       a[index]:=i;
        if check(index) then    
          recursion(index+1);
       end;
      
    end;
end;
begin
writeln('Program for writing permutations');
writeln('of n elements in a specified file !');
writeln('How much elements (chars) do you want ?');
readln(n);
for i:=1 to n do
begin
  write(i,' = ');
  readln(s[i]);
end;
writeln('Enter location of output file ?');
readln(location);
assign(out,location);
rewrite(out);
recursion(1);
writeln('Process completed !');
close(out);
end.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
07-27-2009, 03:31 AM
Post: #2
RE: Permutations of elements
Impressive yet another algorithm that makes good use of recursion!

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
07-27-2009, 06:07 AM (This post was last modified: 07-27-2009 06:09 AM by drdebcol.)
Post: #3
RE: Permutations of elements
(07-27-2009 03:31 AM)Back_track Wrote:  Impressive yet another algorithm that makes good use of recursion!
Yeah it basically tries every possible combination with class n !
And then checks if one number was used before, and if it is, it brakes that process and goes to another until it gets n different numbers in array, and then
it just writes char array in order of integer array !
Number of permutations = factorial(n) .
n ! = 1*2*3*...*n

BTW This is 700th post on this forum !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: