Skip to content

Commit ab34f0f

Browse files
committed
Merge branch 'master' of git://github.com/laucer/java-algorithms-implementation into laucer-master
2 parents 7baacfb + 6949403 commit ab34f0f

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

Permutations.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.jwetherell.algorithms.mathematics;
2+
3+
import java.util.LinkedList;
4+
5+
6+
/*
7+
* Recursive permutations generating
8+
* @author Lucjan Roslanowski <lucjanroslanowski@gmail.com>
9+
*
10+
* Example of usage:
11+
* int numbers[] = {7,5,3};
12+
* LinkedList<LinkedList<Integer>> result = new LinkedList<LinkedList<Integer>>();
13+
* getAllPermutations(numbers,result);
14+
*/
15+
16+
public class Permutations {
17+
18+
public static LinkedList<LinkedList<Integer>> getAllPermutations(final int[] numbers, LinkedList<LinkedList<Integer>> result){
19+
//numbers given in an array are also a permutation
20+
LinkedList<Integer> firstPermutation = new LinkedList<>();
21+
for(int el : numbers){
22+
firstPermutation.add(new Integer(el));
23+
}
24+
result.add(firstPermutation);
25+
//let's permute all elements in array starting from index 0
26+
return permute(numbers, 0, result);
27+
}
28+
29+
public static LinkedList<LinkedList<Integer>> permute(final int[] numbers,
30+
int currentElementIndex, LinkedList<LinkedList<Integer>> result){
31+
if(currentElementIndex == numbers.length - 1){
32+
return result;
33+
}
34+
35+
for(int i = currentElementIndex; i < numbers.length; ++i){
36+
//swapping two elements
37+
int temp = numbers[i];
38+
numbers[i] = numbers[currentElementIndex];
39+
numbers[currentElementIndex] = temp;
40+
41+
permute(numbers, currentElementIndex + 1,result);
42+
43+
//all next permutation found
44+
if(i != currentElementIndex){
45+
LinkedList<Integer> nextPermutation = new LinkedList<>();
46+
for(int j = 0; j < numbers.length; j++)
47+
nextPermutation.add(new Integer(numbers[j]));
48+
result.add(nextPermutation);
49+
}
50+
51+
//swapping back two elements
52+
temp = numbers[i];
53+
numbers[i] = numbers[currentElementIndex];
54+
numbers[currentElementIndex] = temp;
55+
}
56+
return result;
57+
}
58+
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.jwetherell.algorithms.mathematics.test;
2+
3+
import com.jwetherell.algorithms.mathematics.Permutations;
4+
import static org.junit.Assert.*;
5+
6+
import java.util.LinkedList;
7+
8+
import org.junit.Test;
9+
10+
public class PermutationsTest {
11+
12+
@Test
13+
public void test1NumberOfPermutations() {
14+
int numbers[] = {1,2,3,4};
15+
int expectedNumberOfPermutations = 24;
16+
LinkedList<LinkedList<Integer>> result = new LinkedList<LinkedList<Integer>>();
17+
18+
assertEquals(expectedNumberOfPermutations, (Permutations.getAllPermutations(numbers,result)).size());
19+
}
20+
21+
@Test
22+
public void test2NumberOfPermutations() {
23+
int numbers[] = {3,4,2};
24+
int expectedNumberOfPermutations = 6;
25+
LinkedList<LinkedList<Integer>> result = new LinkedList<LinkedList<Integer>>();
26+
27+
assertEquals(expectedNumberOfPermutations, (Permutations.getAllPermutations(numbers,result)).size());
28+
}
29+
30+
@Test
31+
public void test3NumberOfPermutations() {
32+
int numbers[] = {3,4,2,5,4,9};
33+
int expectedNumberOfPermutations = 720;
34+
LinkedList<LinkedList<Integer>> result = new LinkedList<LinkedList<Integer>>();
35+
36+
assertEquals(expectedNumberOfPermutations, (Permutations.getAllPermutations(numbers,result)).size());
37+
}
38+
39+
@Test
40+
public void testComparePermutations() {
41+
int numbers[] = {4,2};
42+
43+
LinkedList<Integer> firstPermutation = new LinkedList<>();
44+
firstPermutation.add(4);
45+
firstPermutation.add(2);
46+
47+
LinkedList<Integer> secondPermutation = new LinkedList<>();
48+
secondPermutation.add(2);
49+
secondPermutation.add(4);
50+
51+
LinkedList<LinkedList<Integer>> allPermutations = new LinkedList<LinkedList<Integer>>();
52+
allPermutations.add(firstPermutation);
53+
allPermutations.add(secondPermutation);
54+
55+
LinkedList<LinkedList<Integer>> result = new LinkedList<LinkedList<Integer>>();
56+
57+
assertEquals(allPermutations, Permutations.getAllPermutations(numbers,result));
58+
}
59+
}

0 commit comments

Comments
 (0)