Skip to content

Instantly share code, notes, and snippets.

@spceaza
Created September 20, 2015 20:53
Show Gist options
  • Save spceaza/49c19dd1951cdb478b09 to your computer and use it in GitHub Desktop.
Save spceaza/49c19dd1951cdb478b09 to your computer and use it in GitHub Desktop.
public class Permutaciones {
public static void permutacion(int array1[], int array2[]) {
int size1 = array1 == null ? 0 : array1.length;
int size2 = array2.length;
if (size2 == 2) {
/**
* *********************** Caso Base ***********************
*/
System.out.print("[");
for (int i = 0; i < size1; i++)
System.out.print(array1[i] + ",");
System.out.println(array2[0] + "," + array2[1] + "]");
System.out.print("[");
for (int i = 0; i < size1; i++)
System.out.print(array1[i] + ",");
System.out.println(array2[1] + "," + array2[0] + "]");
} else {
/**
* ******************** Caso Recursivo *********************
*/
for (int i = 0; i < size2; i++) {
int array1_temp[] = new int[size1 + 1];
for (int j = 0; j < size1; j++)
array1_temp[j] = array1[j];
array1_temp[size1] = array2[i];
int counter = 0;
int array2_temp[] = new int[size2 - 1];
for (int j = 0; j < size2; j++) {
if (i == j)
continue;
array2_temp[counter++] = array2[j];
}
permutacion(array1_temp, array2_temp);
}
}
}
public static void permutacion(int array1[]) {
int array2[] = null;
permutacion(array2, array1);
}
public static void main(String[] args) {
int size = 3;
int array[] = new int[size];
for (int i = 0; i < size; i++)
array[i] = i + 1;
permutacion(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment