-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitCircle.java
More file actions
227 lines (177 loc) · 6.91 KB
/
UnitCircle.java
File metadata and controls
227 lines (177 loc) · 6.91 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*************************************************************************
* Pace University
*
* Course: CS 608 Algorithms and Computing Theory
* Author: Heli Rawal
* Collaborators: None
* References: None
* Project: 3
* Problem: Testing the runnning time .
* Description: This program measures the running time of QuickSort and BucketSort Algorithm for n points of a unit circle
with radius 1.
* Input: n points p=(x,y) of the unit circle with radius 1.
* Output: The running time taken by BucketSort and Quicksort to sort the n input points.
.
* Visible data fields:
* none.
* Visible methods:
* public void quickSort(double[] array, int low, int high)
* public void BucketSort(double[] array, double maxValue)
* Remarks
* -------
4) By Observing the table below it can be said that the running time for the QuickSort is O(nlogn) and BucketSort is O(n).
Also, it can be observed that for the larger inputs QuickSort runs faster than BucketSort that is because in BucketSort
major portion of processing time is wasted on creating buckets, sorting data in them, and moving the sorted elements from
the buckets to the resultant array. Thus, although Bucket Sort requires fewer comparisons than Quick Sort, due to different
overhead expenses it consumes much more time than expected. Also, when it comes for smaller inputs, Bucket Sort may be a better
choice as it has fewer comparisions to make than QuickSort.
* Chart of running times observed in nanoseconds:
*
* Construction Time:
*
* | 100 | 1000 | 10000 | 100000
* ---------------------------------------------------------------
* QuickSort | 583815 | 4787644 | 9609702 | 57033339
* ---------------------------------------------------------------
* BucketSort| 48763 | 2291172 | 10035819 | 223771099
* ---------------------------------------------------------------
*************************************************************************/
import java.util.Scanner;
public class UnitCircle {
public void quickSort(double[] array, int low, int high) {
//check for null array
if (array == null || array.length == 0) {
return;
}
if (low >= high) {
return;
}
//Choose pivot the middle element of an array
int middle = low + (high - low) / 2;
Double pivot = array[middle];
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
//Loop until all values on left side array are lower than pivot
while (array[i] < pivot) {
i++;
}
//Loop until all values on left side array are greater than pivot
while (array[j] > pivot) {
j--;
}
//Now compare values from both side of lists to see if they need swapping
//After swapping move the iterator on both lists
if (i <= j) {
swap(array, i, j);
i++;
j--;
}
}
//Do same operation as above recursively to sort two sub arrays
if (low < j) {
quickSort(array, low, j);
}
if (high > i) {
quickSort(array, i, high);
}
}
public static void swap(double[] array, int x, int y) {
Double temp = array[x];
array[x] = array[y];
array[y] = temp;
}
//BucketSort Implementation for n input points
public void BucketSort(double[] array, double maxValue) {
double n = (int) array.length;
double[] Bucket = new double[(int)n];
//Creating an empty bucket
for (int i = 1; i < n; i++) {
Bucket[(int) array
[i]]++;
}
//Storing values into the bucket
for (int i = 1; i < n; i++) {
if (array[i] < 1) {
double distance = Math.pow(array[i], 2);
int abs = (int) Math.abs(distance * maxValue);
Bucket[abs] = array[i];
} else if (array[i] == 1) {
Bucket[(int) (maxValue - 1)] = array[i];
}
}
//Sorting the bucket with multiple values
for (int i = 0; i <= n - 1; i++) {
Bucket = insertionSort(Bucket);
}
}
private double[] insertionSort(double[] arr) {
for (int i = 1; i < arr.length; ++i) {
double key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
return arr;
}
private static int maxVal(double[] array) {
int maxValue = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] > maxValue) {
maxValue = (int) (array[i] * 10);
}
}
return maxValue;
}
// Main method to test the code
public static void main(String[] args) {
System.out.println("Enter number of n points:");
Scanner input = new Scanner(System.in);
int user = input.nextInt();
double[] array = new double[user];
UnitCircle uc = new UnitCircle();
//Start of Generating and printing numbers of array
for (int i = 0; i < user; i++) {
double t, r, u, x, y, distance;
t = (2 * Math.PI * Math.random());
u = Math.random() + Math.random();
if (u > 1) {
r = 2 - u;
} else {
r = u;
}
x = r * Math.cos(t);
y = r * Math.sin(t);
distance = Math.sqrt(Math.pow((x), 2) + Math.pow((y), 2));
array[i] = distance;
}
//Testing runtime of QuickSort
long startTime = System.nanoTime();
uc.quickSort(array, 0, array.length - 1);
System.out.println("The time taken by QuickSort is: " + (System.nanoTime() - startTime) + " nanoseconds.");
//Bucket Sort
//Start of Generating and printing numbers of array
for (int i = 0; i < user; i++) {
double t, r, u, x, y, distance;
t = (2 * Math.PI * Math.random());
u = Math.random() + Math.random();
if (u > 1) {
r = 2 - u;
} else {
r = u;
}
x = r * Math.cos(t);
y = r * Math.sin(t);
distance = Math.sqrt(Math.pow((x), 2) + Math.pow((y), 2));
array[i] = distance;
}
int maxValue = maxVal(array);
//Testing runtime of BucketSort
startTime = System.nanoTime();
uc.BucketSort(array, 1);
System.out.println("The time taken by BucketSort is: " + (System.nanoTime() - startTime) + " nanoseconds.");
}
}