Combinatoric Permutations Algorithm



//By Alex McAlpine
//CSC-134 Charles Chen
//02-04-2003
//This program gives every possible integer combo for any number of given integers.
//Permutations.exe ver1.0.2
//PreProcessor Directives

#include 
#include 
#include 

//Function Prototypes
void Aquire(void);
int Mixer(int,int,int[],bool[],int[]);

//structures
struct numlist
        {
                int value;
                struct numlist *next;
        };

//Global Variables
int counter = 0;

//List Pointers
numlist *head;
numlist *prev;
numlist *now;

//MAIN FUNCTION
int main()
        {
                Aquire();
                                                        //Assembling Boolian dummyArray for permutation tests assuring no repeats
                bool *dummyB = new bool[counter];
                                                        //Assembling integer Array for results
                int *temp = new int[counter];
                
                for(int index = 0;index < counter;index++)
                        {
                                dummyB[index] = 1;
                        }
                                                        //Definition of Ascending Sort Algorithm
                cout<value;
                                now = now->next;
                        }
                                                        // Freeing the Memory
                struct numlist *buffer;
                now = head;
                
                while(now)
                        {
                                buffer = now->next;
                                delete now;
                                now = buffer;
                        }
                                                        //Organizing from smallest to greatest with additive index sort
                for(int index = 0;index < counter - 1; index++)
                        {
                                for(int index2 = index + 1; index2 < counter;index2++)
                                        {
                                                if(numArray[index] > numArray[index2])
                                                        {
                                                                numArray[index] = numArray[index] + numArray[index2];
                                                                numArray[index2] = numArray[index] - numArray[index2];
                                                                numArray[index] = numArray[index] - numArray[index2];
                                                        }
                                        }
                        }
                for(int index = 0;index < counter; index++)
                        {
                                cout< this is not in a function because of scope problums
                                                        //with numArray.
                Mixer(0,counter,temp,dummyB,numArray);
                                                        //cleaning up the rest of the dynamicly allocated variables.
                delete dummyB;
                delete numArray;
                delete temp;
                system("PAUSE");
                
                return 0;
        }
                                                        //Function definitions
                                                        //Definition of Aquire()
inline void Aquire(void)
        {
                int Input = 0 ;
                cout<<"Please enter any given number of integers.\n";
                cout<<"After each integer press enter, when you are\n";
                cout<<"done enter 0\n";
                                                        //Loop until user enters 0.
                do
                        {
                                cin>>Input;
                                if(Input)
                                        {
                                                counter++;
                                                now = new numlist;
                                                if(now == 0)
                                                        {
                                                                cout<<"Cannot Allocate Memory\nProgram Variables Will Be Lost";
                                                                exit(0);
                                                        }
                                                now->value = Input;
                                                
                                                if(prev == 0)
                                                        {
                                                                head = now;
                                                        }
                                                else
                                                        {
                                                                prev->next = now;
                                                        }
                                                now->next = 0;
                                                prev = now;
                                        }
                        }
                        
                while(Input);
                                                        //Calculating the number of possible combinations
                unsigned short ComboView = 1;
                for(unsigned short step = counter;step > 0;step--)
                        {
                                ComboView*= step;
                        }

                        
                cout<<"There are "<< ComboView<<" possible combinations for "<< counter<<" different\n";
                cout<<"integers."<< endl;
        }
                                                        //Definition of Combinatoric Permutation Algorithm, using Recursion.
inline int Mixer(int list,int max,int tempArray[],bool dummy[],int ARRAY[])
        {
                if(list == max)
                        {
                                for(int index = 0; index < max;index++)
                                        {
                                                cout<< tempArray[index]<<" ";
                                        }
                                cout<< endl;
                        }
                else
                        {
                                for(int mloop = 0;mloop < max;mloop++)
                                        if(dummy[mloop] == 1)
                                                {
                                                        dummy[mloop] = 0;
                                                        tempArray[list] = ARRAY[mloop];
                                                        Mixer(list+ 1,max,tempArray,dummy,ARRAY);
                                                        dummy[mloop] = 1;
                                                }
                                                        //list = 0;
                        }
                return 1;
        }



ALEXORT


#include 

//Global Variable Declarations
int group[200] ;
static short int indexa = 0;
static short int indexb = indexa +1;

//Function Prototypes
int Alexort(void); //Recursive Search using Globals

int main(void)
	{
		printf("Hello World\nTest Program\n");


		printf("Unsorted list:\n");
		for(int i = 0 ; i < 200; i++)
			{
				group[i] = i + 1;
				printf("%i\n",group[i]);
			}//END FOR Charging array

		printf("Sorting...\n");
		Alexort();
		
		for(int i = 0 ; i < 200; i++)
			{
				printf("%i\n",group[i]);
			}//END FOR PRINTING SORTED LIST

	} //END MAIN() Function






int Alexort()
	{
		if(indexa >= 199)
			{
				return 1;
			}

		if(group[indexa] < group[indexb])
			{
				group[indexa]  = group[indexa] + group[indexb];
				group[indexb] = group[indexa] - group[indexb];
				group[indexa] = group[indexa] - group[indexb];
			}
		
		indexb += 1;

		if(indexb >= 200)
			{
				indexa += 1;
				indexb = indexa + 1;
			}

		

		Alexort();

	}//END Alexort() Function



Python __Alexort() uses a charged Array newMatrix[]

def Alexort():
    global indexa
    global indexb

    if indexa >= len(newMatrix) - 1:
        return 1

    if newMatrix[indexa] < newMatrix[indexb]:
        newMatrix[indexa] = newMatrix[indexa] + newMatrix[indexb]
        newMatrix[indexb] = newMatrix[indexa] - newMatrix[indexb]
        newMatrix[indexa] = newMatrix[indexa] - newMatrix[indexb]


    indexb += 1

    if indexb >= len(newMatrix):
        indexa += 1
        indexb = indexa + 1

    Alexort()



Download VBA Premutator
Download Windows 2000 and up Premutator
Back to BrainLubeOnline