Skip to content

Commit c548c5f

Browse files
author
codelogicws
committed
Finished Shuffle
1 parent 95092b9 commit c548c5f

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed
Binary file not shown.
Binary file not shown.
52 Bytes
Binary file not shown.
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
package ws.codelogic.algorithms.shuffle;
22

3+
import java.util.Random;
4+
35
public class Shuffler {
46

7+
private Random random = new Random();
8+
private int[] shuffledArray;
9+
510
public int[] shuffle(int[] sorted) {
6-
return new int[4];
11+
shuffledArray = sorted.clone();
12+
for(int i=0;i<sorted.length;i++){
13+
swap(i, random(i));
14+
}
15+
printShuffledArray(sorted);
16+
return shuffledArray;
17+
}
18+
19+
private void swap(int i, int random) {
20+
int temp = shuffledArray[i];
21+
shuffledArray[i] = shuffledArray[random];
22+
shuffledArray[random] = temp;
23+
}
24+
25+
private int random(int index){
26+
if(index > 0){
27+
return random.nextInt(index);
28+
}
29+
return 0;
30+
}
31+
32+
private void printShuffledArray(int[] array){
33+
for(int i : array){
34+
System.out.print(i + ", ");
35+
}
736
}
837
}

src/ws/codelogic/algorithms/shuffle/ShufflerTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@ public void setup() {
1717
@Test
1818
public void testItemsAreNotInTheSortedOrderTheyWhereSentIn() {
1919
int[] shuffled = shuffler.shuffle(sorted);
20-
assertFalse(arraysNotEqual(shuffled, sorted));
20+
assertTrue(arraysNotEqual(shuffled, sorted));
2121
}
2222

2323
private boolean arraysNotEqual(int[] array1, int[] array2){
2424
boolean arraysEqual = true;
2525
if(array1.length != array2.length){
2626
arraysEqual = false;
2727
}else{
28-
for(int i=0;i<array1.length;i++){
29-
if(array1[i] != array2[i]){
30-
arraysEqual = false;
31-
break;
32-
}
28+
arraysEqual = checkForMatchs(array1, array2, arraysEqual);
29+
}
30+
return !arraysEqual;
31+
}
32+
33+
private boolean checkForMatchs(int[] array1, int[] array2, boolean arraysEqual) {
34+
for(int i=0;i<array1.length;i++){
35+
if(array1[i] != array2[i]){
36+
arraysEqual = false;
37+
break;
3338
}
3439
}
3540
return arraysEqual;

src/ws/codelogic/test/TestSuite.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import ws.codelogic.algorithms.Stack.LinkStackTest;
77
import ws.codelogic.algorithms.arithmetic.evaluation.DijkstrasTwoStackTest;
88
import ws.codelogic.algorithms.search.SearchTest;
9-
import ws.codelogic.algorithms.shuffle.Shuffler;
9+
import ws.codelogic.algorithms.shuffle.ShufflerTest;
1010
import ws.codelogic.algorithms.sort.InsertionSortTest;
1111
import ws.codelogic.algorithms.sort.SelectionSortTest;
12-
import ws.codelogic.algorithms.sort.ShellSort;
1312
import ws.codelogic.algorithms.sort.ShellSortTest;
1413
import ws.codelogic.algorithms.union.UnionTest;
1514

@@ -22,7 +21,7 @@
2221
ShellSortTest.class,
2322
DijkstrasTwoStackTest.class,
2423
ArrayStackTest.class,
25-
// Shuffler.class,
24+
ShufflerTest.class,
2625
LinkStackTest.class})
2726
public class TestSuite {
2827
}

0 commit comments

Comments
 (0)