View Single Post
Old 12-27-2005, 09:06 PM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Here's a recursive permute_array function. (In psuedo code cause I don't generally do java)
Code:
int permute (int[] input_array, int[] output_array, int input_array_length) {
     //input array should be pass by value
     //output array should be pass by reference
     if (input_array_length == 0) {
	 //Add the current permutation of the vector to the output array
	 output_array.push( vector_to_int( input_array ));
     }
     else {
	 for( i = 0; i < input_array_length; i++) {
	     //run permute with two elements swapped
	     int tmp1 = input_array[i];
	     int tmp2 = input_array[input_array_length - 1];
	     input_array[i] = tmp2;
	     input_array[input_array_length - 1] = tmp1;
	     permute( input_array, output_array, input_array_length - 1 );
	     //Undo swap for next iteration.
	     tmp1 = input_array[i];
	     tmp2 = input_array[input_array_length - 1];
	     input_array[i] = tmp2;
	     input_array[input_array_length - 1] = tmp1; 
	 }
     }
}

int vector_to_int (int[] vec) {
    //turns [1,2,3] into 321
    int out = 0;
    for (i = 0; i < vec.length; i++) {
	out += vec[i] * pow(10, i); //don't know how java does exponents but what I mean to say is 10 to power of i
    }
    return out;
}
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote