-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.java
More file actions
45 lines (41 loc) · 1.12 KB
/
quick_sort.java
File metadata and controls
45 lines (41 loc) · 1.12 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
public class quick_sort extends Sorts{
public quick_sort(int[] parameters)
{
super(parameters, parameters.length, "Quick Sort");
}
@Override
public void sort()
{
startTime = System.nanoTime();
quick(0, size-1);
endTime = System.nanoTime();
duration = endTime - startTime;
}
public void quick(int low, int high)
{
if (low < high) {
int index = partition(low, high);
quick(low, index-1);
quick(index+1, high);
}
}
public int partition(int low, int high)
{
int pivot = parameters[high];
int first = low, end = low;
for(;end<high;end++)
{
if (parameters[end] < pivot) {
swap(first++, end);
duration = System.nanoTime() - startTime;
saveTimeState();
saveState();
}
}
swap(first, high);
duration = System.nanoTime() - startTime;
saveTimeState();
saveState();
return first;
}
}