File tree 1 file changed +19
-8
lines changed
src/designPattern/strategy
1 file changed +19
-8
lines changed Original file line number Diff line number Diff line change 3
3
import java .util .Arrays ;
4
4
5
5
public class RandomSort implements SorterIF {
6
+ int [] sorted ;
6
7
7
8
@ Override
8
9
public int [] sort (int [] sourceArray ) {
10
+ sorted = Arrays .copyOf (sourceArray , sourceArray .length );
11
+ Arrays .sort (sorted );
12
+ long time = System .currentTimeMillis ();
9
13
int [] targetArray = Arrays .copyOf (sourceArray , sourceArray .length );
14
+ int i = 0 ;
10
15
while (!isSorted (targetArray )) {
11
16
shuffle (targetArray );
17
+ if (i %10000000 == 0 && i != 0 ) {
18
+ System .out .println ("Zwischenstand " + i + " in " + (System .currentTimeMillis () - time ) / 1000.0 );
19
+ }
20
+ i ++;
12
21
}
13
22
return targetArray ;
14
23
}
15
24
16
25
private void shuffle (int [] i ) {
17
- for (int x = 0 ; x < i .length ; ++ x ) {
26
+ for (int x = 0 ; x < i .length ; x ++ ) {
18
27
int index1 = (int ) (Math .random () * i .length );
19
- int index2 = (int ) (Math .random () * i .length );
20
- int a = i [index1 ];
21
- i [index1 ] = i [index2 ];
22
- i [index2 ] = a ;
28
+ int a = i [x ];
29
+ i [x ] = i [index1 ];
30
+ i [index1 ] = a ;
23
31
}
24
32
}
25
33
26
- private boolean isSorted (int [] i ){
27
- for (int x = 0 ; x < i .length - 1 ; ++x ) {
28
- if (i [x ] > i [x + 1 ]) {
34
+ private boolean isSorted (int [] toCheck ){
35
+ if (toCheck .length == 1 ) {
36
+ return true ;
37
+ }
38
+ for (int i = 1 ;i < toCheck .length ; i ++) {
39
+ if (toCheck [i - 1 ] > toCheck [i ]) {
29
40
return false ;
30
41
}
31
42
}
You can’t perform that action at this time.
0 commit comments