forked from liuyubobobo/Play-with-Algorithm-Visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSortData.java
56 lines (42 loc) · 1.41 KB
/
InsertionSortData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Arrays;
public class InsertionSortData {
public enum Type{
Default,
NearlyOrdered
}
private int[] numbers;
public int orderedIndex; // [0...orderedIndex) 是有序的
public int currentIndex;
public InsertionSortData(int N, int randomBound, Type dataType){
numbers = new int[N];
for( int i = 0 ; i < N ; i ++)
numbers[i] = (int)(Math.random()*randomBound) + 1;
if(dataType == Type.NearlyOrdered){
Arrays.sort(numbers);
int swapTime = (int)(0.02*N);
for(int i = 0 ; i < swapTime; i ++){
int a = (int)(Math.random()*N);
int b = (int)(Math.random()*N);
swap(a, b);
}
}
}
public InsertionSortData(int N, int randomBound){
this(N, randomBound, Type.Default);
}
public int N(){
return numbers.length;
}
public int get(int index){
if( index < 0 || index >= numbers.length)
throw new IllegalArgumentException("Invalid index to access Sort Data.");
return numbers[index];
}
public void swap(int i, int j) {
if( i < 0 || i >= numbers.length || j < 0 || j >= numbers.length)
throw new IllegalArgumentException("Invalid index to access Sort Data.");
int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
}