Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,305 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,457 people online right now. Registration is fast and FREE... Join Now!




permutation in place

 
Reply to this topicStart new topic

permutation in place, without using a second array do permutation

ashisherudite
15 Oct, 2008 - 09:18 AM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2008
Posts: 3

hi all , i am trying for implementing code for permutation in place i have written code after reading some research paper on that topic but i am getting some difficulty for understanding cycle leader and cycle structure in permutation task is rearrange the array element without using other array i m showing my code here i am trying to understand one function pi() which is mean there // permutation in oracle but i did not get actual meaning and did not getting correct answer plase help me for that problem as soon as possible. i am just takink my imagination in definition of pi() fun. may be it wrong.. ...

CODE



#include<iostream.h>
#include<conio.h>
int n=4;
int  a[4]={1,2,3,4};

int *ar[4]; /* the array to be permuted */
*ar[] = &a[];
void exchange (int i ,int j)
{
  int temp=*ar[i];
     *ar[i]=*ar[j];
      *ar[j]=temp;
}
int pi(int x) /* permutation as oracle */
{
    switch(x)
    {
    case 0: return 3;
        break;
    case 1: return 1;
        break;
    case 2: return 2;
        break;
    case 3: return 0;
        break;
    default : cout<<"no exist pos.";
        break;
    }
}
int IsLeader(int i);

void PermuteCycle(int i)
{
    int j=i; /* loop variable */
    do
     {
       j=pi(j); /* follow cycle */
       exchange(i,j); /* exchange a[i] and a[j] */
     }
          while (j!=i);
}

void main(void)
{
  clrscr();
  int i,ret; /* loop variable */
  for(i=0;i<4;i++)
  cout<<" "<<a[i];
  cout<<endl;
  for(i=0;i<n;i++)
   {    ret=IsLeader(i);
            if(ret==1)
                PermuteCycle(i);
   }
  for(i=0;i<4;i++)
  cout<<" "<<a[i];
  cout<<endl;

  getch();
}

int TestHypo(int j,int i)
{
  return i<=j;
}

int IsLeader(int i)
{
int j=i,ret; /* loop variable */
int flag=1; /* assume i is leader */
do
  {
    j=pi(j); /* follow cycle */
    ret=TestHypo(j,i);
    if(flag==ret)
    {   //flag=0;
        break;
    }
    /* until assumption turns out false */
  }
  while (j!=i); /* or cycle is completely traversed */
return flag;
}


~Added code tags
-jjsaw5
User is offlineProfile CardPM
+Quote Post

jjsaw5
RE: Permutation In Place
15 Oct, 2008 - 09:38 AM
Post #2

I must break you
Group Icon

Joined: 4 Jan, 2008
Posts: 1,394



Thanked: 6 times
Dream Kudos: 125
Expert In: HTML, CSS, Database,

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:33AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month