-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.java
More file actions
195 lines (160 loc) · 5.54 KB
/
Sorting.java
File metadata and controls
195 lines (160 loc) · 5.54 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package sorting;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
public class Sorting {
/** Runner for comparing 4 sorting algorithms
*
* 1. Arrays.sort() implemented with TimSort
* 2. aviSort() implemented with custom algorithm
* 3. aviSort2() optimized version of aviSort()
* 4. radix_array_sort() implementation of radix sort
* Code found from educba.com: https://www.educba.com/radix-sort-java/
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int numVals= 10;
int stepSize= 1;
int[] b= new int[numVals];
ArrayList<Integer> list= new ArrayList<>(numVals);
for (int i= 1; i <= numVals; i++ ) {
list.add(i - 1);
}
Random rand= new Random();
int k= 0;
while (list.size() > 0) {
int index= rand.nextInt(list.size());
b[k]= list.remove(index);
k++ ;
}
double time= 0;
HashMap<Integer, Integer> d;
time= System.nanoTime();
Arrays.sort(b);
System.out.println(System.nanoTime() - time);
time= System.nanoTime();
d= aviSort(b, stepSize);
System.out.println(System.nanoTime() - time);
time= System.nanoTime();
d= aviSort2(b, stepSize);
System.out.println(System.nanoTime() - time);
time= System.nanoTime();
radix_array_sort(b, b.length);
System.out.println(System.nanoTime() - time);
}
/** Sorts array b inside of the HashMap ADT
*
* @param b the integer array to be sorted
* @param stepSize the bin increment size
* @return a sorted version of array b stored in a HashMap The HashMap maps each integer value
* to its sorted place in the array */
public static HashMap<Integer, Integer> aviSort(int[] b, int stepSize) {
double time= System.nanoTime();
HashSet<Integer> c= prepSort(b);
int m= minValue(b);
HashMap<Integer, Integer> d= new HashMap<>();
int i= m;
int valsUsed= 0;
int size= c.size();
while (valsUsed < size) { // !c.isEmpty()) {
if (c.remove(i)) {
d.put(i, d.size());
}
i+= stepSize;
valsUsed++ ;
}
return d;
}
/** Puts each value of the integer array b in a HashSet
*
* Only used in the first version of "aviSort", later deemed unnecessary in more optimized
* version
*
* @param b the integer array to be sorted
* @return a HashSet containing all the values in b */
public static HashSet<Integer> prepSort(int[] b) {
HashSet<Integer> c= new HashSet<>();
for (int elm : b)
c.add(elm);
return c;
}
/** Finds the minimum value in array b in O(n) time
*
* @param b the integer array
* @return the minimum value in b */
public static int minValue(int[] b) {
int minVal= b[0];
for (int elm : b)
if (elm < minVal)
minVal= elm;
return minVal;
}
/** Sorts array b inside of the HashMap ADT
*
* @param b the integer array
* @param stepSize the amount to increment each bin size
* @return a sorted version of array b stored in a HashMap The HashMap maps each integer value
* to its sorted place in the array */
public static HashMap<Integer, Integer> aviSort2(int[] b, int stepSize) {
int m= minValue(b);
HashMap<Integer, Integer> d= new HashMap<>();
for (int elm : b)
d.put(elm, null);
int i= m;
int valsUsed= 0;
int size= d.size();
while (valsUsed < size) {
d.replace(i, valsUsed);
i+= stepSize;
valsUsed++ ;
}
return d;
}
/** Method to obtain maximum value in radixArr[] (used in countSorting)
*
*
* Code found from educba.com: https://www.educba.com/radix-sort-java/
*
* Using their implementation to compare */
static int get_maxVal(int radixArr[], int arrLen) {
int maxVal= radixArr[0];
for (int i= 1; i < arrLen; i++ )
if (radixArr[i] > maxVal)
maxVal= radixArr[i];
return maxVal;
}
/** Performs count sorting on radixArr
*
*
* Code found from educba.com: https://www.educba.com/radix-sort-java/
*
* Using their implementation to compare to mine */
static void countSorting(int radixArr[], int arrLen, int exp) {
int resultArray[]= new int[arrLen];
int i;
int countVal[]= new int[10];
Arrays.fill(countVal, 0);
for (i= 0; i < arrLen; i++ )
countVal[radixArr[i] / exp % 10]++ ;
for (i= 1; i < 10; i++ )
countVal[i]+= countVal[i - 1];
for (i= arrLen - 1; i >= 0; i-- ) {
resultArray[countVal[radixArr[i] / exp % 10] - 1]= radixArr[i];
countVal[radixArr[i] / exp % 10]-- ;
}
for (i= 0; i < arrLen; i++ )
radixArr[i]= resultArray[i];
}
/** Organize radix sort using count sorting
*
* Code found from educba.com: https://www.educba.com/radix-sort-java/
*
* Using their implementation to compare to mine */
static void radix_array_sort(int radixArr[], int arrLen) {
int m= get_maxVal(radixArr, arrLen);
for (int exp= 1; m / exp > 0; exp*= 10)
countSorting(radixArr, arrLen, exp);
}
}