-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.java
More file actions
56 lines (53 loc) · 1.95 KB
/
quick_sort.java
File metadata and controls
56 lines (53 loc) · 1.95 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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class quick_sort {
private static final int THREAD_COUNT = Runtime.getRuntime().availableProcessors();
public static int partition(int[] arr, int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
public static void quick(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quick(arr, low, pi - 1);
quick(arr, pi + 1, high);
}
}
/*
* Implemented Multithreading :)(03/02/2025)(@ a very young evening)
*/
//1 File = 490 seconds (Single Threaded)
//10 Files = 32.29 seconds (Multithreaded + BufferedWriter)
public static void main(String[] args) {
long startTimeFull = System.nanoTime();
ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
for (int i = 1; i <= 10; i++) {
int fileIndex = i;
executor.execute(() -> utils.processFile("output/quick/output", fileIndex, quick_sort::quick));
}
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (Exception e) {
System.out.println("Executor Interrupted!");
e.printStackTrace();
}
Runtime.getRuntime().gc();
System.out.println("Full Processing Time(ns): " + (System.nanoTime() - startTimeFull) + " ns");
System.out.println("Full Processing Time(s): " + (System.nanoTime() - startTimeFull) / 1e9 + " s");
}
}