-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearTimeSorting.java
192 lines (144 loc) · 5.26 KB
/
LinearTimeSorting.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
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
class LinearTimeSorting
{
ArrayList<Float> bucketSort(ArrayList<Float> arr,int bucketSize) {
float max=0;
int divider;
int numberOfBuckets = bucketSize;//(int) (1.5*(size));
float min=Float.MAX_VALUE;
ArrayList<Float> [] B=new ArrayList[numberOfBuckets];
ArrayList<Float> nonNegative=new ArrayList<>();
ArrayList<Float> negative=new ArrayList<>();
for (float x:
arr) {
if(x>max)max=x;
if(x<min)min=x;
if(x>=0)nonNegative.add(x);
else negative.add(-x);
}
for (int l = 0; l <numberOfBuckets ; l++) {
B[l]=new ArrayList<>();
}
int size=nonNegative.size();
divider = (int) Math.ceil( (float)(max + 1) / numberOfBuckets);
for(int i = 0,j=0; i < size; i++) {
j = (int) Math.floor( nonNegative.get(i) / divider );
B[j].add(nonNegative.get(i));
}
for(int i = 0; i < numberOfBuckets; i++) {
Collections.sort(B[i]);
}
int k = 0;
for(int i = 0; i < numberOfBuckets; i++) {
for(int j = 0; j < B[i].size(); j++) {
nonNegative.set(k++, B[i].get(j));
}
}
if(min>=0)return nonNegative;
negative=bucketSort(negative,bucketSize);
for (int i =0;i<negative.size() ; i++) {
nonNegative.add(0,-negative.get(i));
}
arr.clear();
arr.addAll(nonNegative);
return arr;
}
ArrayList<Integer> countingSort(ArrayList<Integer> arr)
{
int n = arr.size();
int max=0;
int min=Integer.MAX_VALUE;
ArrayList<Integer> nonNegative=new ArrayList<>();
ArrayList<Integer> negative=new ArrayList<>();
for (int x:
arr) {
if(x>max)max=x;
if(x<min)min=x;
if(x>=0)nonNegative.add(x);
else negative.add(-x);
}
max++;//since we need one more position than the max value
// for (int x:
// arr) {
// if(x>=0)nonNegative.add(x);
// else negative.add(-x);
// }
int output[] = new int[n];
int countNonNegative[] = new int[max];
for (int i=0; i<max; ++i)
countNonNegative[i] = 0;
for (Integer aNonNegative : nonNegative) ++countNonNegative[aNonNegative];
for (int i=1; i<max; ++i)
countNonNegative[i] += countNonNegative[i-1];
for (Integer aNonNegative : nonNegative) {
output[countNonNegative[aNonNegative] - 1] = aNonNegative;
--countNonNegative[aNonNegative];
}
for (int i = 0; i<nonNegative.size(); ++i)
nonNegative.set(i,output[i]);
arr.clear();
arr.addAll(nonNegative);
if(min>=0)return arr;
int countNegative[]=new int[-(min)];
//System.out.println("Negative Elements sorted");
negative=countingSort(negative);
for (int i =0;i<negative.size() ; i++) {
nonNegative.add(0,-negative.get(i));
}
arr.clear();
arr.addAll(nonNegative);
return arr;
}
public static ArrayList<Float> createRandomArray(int size)
{
ArrayList<Float> arr=new ArrayList<>();
for (int i=0;i<size;i++)
{
Random r=new Random();
// arr.add( r.nextInt((int) (max + 1 -min))/size +min);
arr.add((r.nextFloat()*2-1)*size);
}
return arr;
}
// Driver method
public static void main(String args[])
{
LinearTimeSorting ob = new LinearTimeSorting();
int testArrSize;
Scanner sc=new Scanner(System.in);
System.out.println("Enter test Array Size");
testArrSize=sc.nextInt();
ArrayList<Float> arr= LinearTimeSorting.createRandomArray(testArrSize);
ArrayList <Integer> arr2=new ArrayList<>();
for (float d:
arr) {
arr2.add((int )d);
}
System.out.println("Randomly Created Array of size "+testArrSize);
System.out.println(arr);
long s,e;
s=System.currentTimeMillis();
System.out.println("Sorted :");
System.out.println(ob.countingSort(arr2));
e=System.currentTimeMillis();
System.out.println("Time Taken For Counting Sort (n) :"+(e-s)+"ms \n\n");
s=System.currentTimeMillis();
System.out.println("\n\nSorted :");
System.out.println(ob.bucketSort(arr,(int)(1.5*testArrSize)));
e=System.currentTimeMillis();
System.out.println("Time Taken For Bucket Size 1.5n :"+(e-s) +"ms \n\n");
s=System.currentTimeMillis();
System.out.println("Sorted :");
System.out.println(ob.bucketSort(arr,testArrSize));
e=System.currentTimeMillis();
System.out.println("Time Taken For Bucket Size (n) :"+(e-s)+"ms \n\n");
s=System.currentTimeMillis();
System.out.println("Sorted :");
System.out.println(ob.bucketSort(arr,(int)(Math.sqrt(testArrSize))));
e=System.currentTimeMillis();
System.out.println("Time Taken For Bucket Size sqrt(n) :"+(e-s)+"ms \n\n");
}
}