-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglethreadedSorting.java
More file actions
254 lines (185 loc) · 4.85 KB
/
SinglethreadedSorting.java
File metadata and controls
254 lines (185 loc) · 4.85 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.parallel.sort;
//import java.util.Arrays;
import java.util.Random;
@SuppressWarnings("rawtypes")
public class SinglethreadedSorting {
/** Threshold to apply insertion sort */
private static final int THRESHOLD = 100;
private static final int ARRAY_SIZE = 1000000;
//////////////////////////////////////////////////////// quicksort begin
static class QuickSort
{
private Comparable[] a;
public QuickSort(Comparable[] array) { // constructor
a = array;
}
public void sort() {
quicksort(0, a.length - 1);
}
private void quicksort(int low, int high)
{
if (low + THRESHOLD > high)
insertionsort(low, high);
else
{
if(low < high)
{
int partitionIndex = partition(low, high);
quicksort(low, partitionIndex-1);
quicksort(partitionIndex+1, high);
}
}
}
private int partition(int low, int high)
{
Comparable pivot = a[high];
int i = (low-1);
for (int j = low; j < high; j++)
{
if (a[j].compareTo(pivot) <= 0)
{
i++;
Comparable swapTemp = a[i];
a[i] = a[j];
a[j] = swapTemp;
}
}
Comparable swapTemp = a[i+1];
a[i+1] = a[high];
a[high] = swapTemp;
return i+1;
}
private void insertionsort(int low, int high)
{
for (int p = low + 1; p <= high; p++)
{
Comparable tmp = a[p];
int j;
for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--)
a[j] = a[j - 1];
a[j] = tmp;
}
}
}
//////////////////////////////////////////////////////// quicksort end
//////////////////////////////////////////////////////// mergesort begin
static class MergeSort
{
private Comparable[] a;
public MergeSort(Comparable[] array) { // constructor
a = array;
}
public void sort() {
mergesort(0, a.length - 1);
}
private void mergesort(int low, int high)
{
if (low + THRESHOLD > high)
insertionsort(low, high);
else
{
if (low < high) {
int mid = low + (high-low)/2; // find mid
mergesort(low, mid); // sort left half
mergesort(mid + 1, high); // sort right half
merge(low, mid, high); // merge the sorted halves
}
}
}
// Merges two subarrays : arr[low:mid] and arr[mid+1:high]
private void merge(int low, int mid, int high)
{
// Find sizes of two subarrays to be merged
int size1 = mid - low + 1;
int size2 = high - mid;
// Create temporary arrays
Comparable[] left = new Comparable[size1];
Comparable[] right = new Comparable[size2];
// Copy data to temporary arrays
for (int i = 0; i < size1; ++i)
left[i] = a[low + i];
for (int j = 0; j < size2; ++j)
right[j] = a[mid + 1 + j];
// Merge the temporary arrays
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = low;
while (i < size1 && j < size2)
{
if (left[i].compareTo(right[j]) <= 0)
{
a[k] = left[i];
i++;
}
else
{
a[k] = right[j];
j++;
}
k++;
}
// Copy remaining elements of left[] if any
while (i < size1) {
a[k] = left[i];
i++;
k++;
}
// Copy remaining elements of right[] if any
while (j < size2) {
a[k] = right[j];
j++;
k++;
}
}
private void insertionsort(int low, int high)
{
for (int p = low + 1; p <= high; p++)
{
Comparable tmp = a[p];
int j;
for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--)
a[j] = a[j - 1];
a[j] = tmp;
}
}
}
//////////////////////////////////////////////////////// mergesort end
private static Integer[] createArray(final int size) {
Integer[] array = new Integer[size];
Random rand = new Random();
for (int i = 0; i < size; i++) {
array[i] = rand.nextInt(1000);
}
return array;
}
//////////////////////////////////////////////////////// main begin
public static void main(String[] args)
{
long startTime;
long endTime;
// Create two arrays of size ARRAY_SIZE, with the same randomly generated elements
Integer[] array1 = createArray(ARRAY_SIZE);
Integer[] array2 = new Integer[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
array2[i] = array1[i];
}
MergeSort mergeSort = new MergeSort(array1);
// Get the current time before sorting
startTime = System.currentTimeMillis();
mergeSort.sort();
// Get the current time after sorting
endTime = System.currentTimeMillis();
System.out.println("Time taken with MergeSort: " +
(endTime - startTime) + " millis");
QuickSort quickSort = new QuickSort(array2);
// Get the current time before sorting
startTime = System.currentTimeMillis();
quickSort.sort();
// Get the current time after sorting
endTime = System.currentTimeMillis();
System.out.println("Time taken with QuickSort: " +
(endTime - startTime) + " millis");
}
//////////////////////////////////////////////////////// main end
}