-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.java
More file actions
52 lines (47 loc) · 1.94 KB
/
quick_sort.java
File metadata and controls
52 lines (47 loc) · 1.94 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
public class quick_sort {
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);
}
}
public static void main(String[] args) {
long startTimeFull = System.nanoTime();
/*
* I understand that Quicksort is faster than Bubble sort, but 1e6 arrays, nahh, that's a lot of arrays to sort
* at this point, multithreading is the only way to go, but since I am as illiterate as a rock, I won't( can't, *ahem*)
*
* MORAL: Learning algos is cool, but god damn, I am not sorting 1e6 arrays with quicksort single threaded IRL
*/
for (int i = 1; i <= 10; i++) {
int[][] arr = utils.parseArrayFromFile("input" + i + ".txt");
long startTime = System.nanoTime();
for (int j = 0; j < arr.length; j++) {
quick(arr[j], 0, arr[j].length - 1);
utils.writeArrayToFile(arr[j], "quick/ouput" + i + ".txt");
}
System.out.println("Output File " + i + " populated.");
System.out.println("Processing Time(ns): " + (System.nanoTime() - startTime) + " ns");
}
System.out.println("Full Processing Time(ns): " + (System.nanoTime() - startTimeFull) + " ns");
System.out.println("Full Processing Time(s): " + (System.nanoTime() - startTimeFull) / 1e9 + " s");
}
}