-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayGen.java
More file actions
118 lines (94 loc) · 2.69 KB
/
Copy pathArrayGen.java
File metadata and controls
118 lines (94 loc) · 2.69 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Class: ArrayGen
* Author: Mr. Donald
* Date: Nov 13, 2014
*
* Description:
* -A class that randomly creates integer arrays of a given size with the desired distribution.
* -Each method is a class level method.
*
* int[] reversedArray(int size)
* -> Creates an array of size length of all unique values, and returns that array in reversed order
* int[] randomizedArray(int size)
* -> Creates an array of size length of all unique values, and shuffles the array so that the elements are
* in a random order. Returns the shuffled array.
* int[] fewUniqueArray(int size)
* -> Creates an array of size length, and populates it so that each element can only
* be 4 possible values. Returns the array.
* int[] nearlySortedArray(int size)
* -> Creates an array of size length of all unique values, and shuffles small sections of the array,
* so that elements are not in order, but still relatively close to their end position.
*
*/
import java.util.*;
public class ArrayGen
{
//used with to shuffle elements.
public static final Random gen = new Random();
public static int[] reversedArray(int size)
{
int[] a = new int[size];
for (int i = 0; i< size; i++)
{
a[i] = (size-i);
}
return a;
}
public static int[] randomizedArray(int size)
{
int[] a = new int[size];
//Populate array
for (int i = 0; i< size; i++)
{
a[i] = i+1;
}
//Shuffle array using Knuth Shuffle
while (size > 1)
{
int k = gen.nextInt(size--); //decrements after using
int temp = a[size];
a[size] = a[k];
a[k] = temp;
}
return a;
}
public static int[] fewUniqueArray(int size)
{
int[] a = new int[size];
for (int i = 0; i<size; i++)
a[i] = (int)(4*Math.random())+1;
return a;
}
public static int[] nearlySortedArray(int size)
{
int[] a = new int[size];
//create the array
for (int i = 0; i<size; i++)
a[i]=i+1;
//loosly shuffle the data by selecting groups of 3 elements
//and randomly chosing which pairs to switch
for (int i = 0; i<size; i+=3)
{
int ran = (int)(4*Math.random());
if ((ran == 0)&&((i+1)<size))
{
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
else if ((ran == 1)&&((i+2)<size))
{
int temp = a[i+1];
a[i+1] = a[i+2];
a[i+2] = temp;
}
else if ((ran ==2)&&((i+2)<size))
{
int temp = a[i];
a[i] = a[i+2];
a[i+2] = temp;
}
}
return a;
}
}