-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickAlgo.java
97 lines (78 loc) · 2.75 KB
/
QuickAlgo.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
import java.util.ArrayList;
import java.util.Collections;
//source : https://gist.github.com/aaani/6337280
// et https://www.techiedelight.com/hybrid-quicksort/?fbclid=IwAR05cm3-l9aOPoolxpFuyCnM2KRwvnG21wey4bmMy1Ey2INABHfmshPsQfQ
public class QuickAlgo {
private int pivot;
private int threshold;
QuickAlgo (int pivot, int threshold){
// set pivot and threshold
this.pivot = pivot;
this. threshold = threshold;
}
public ArrayList<Long> handle(ArrayList<Long> numbers) {
quicksort(numbers,0, numbers.size()-1);
return numbers;
}
public void swap(ArrayList<Long> elements, int i, int j){
//Method to swap 2 elements in an arraylist
Long temp= elements.get(i);
elements.set(i, elements.get(j));
elements.set(j, temp);
}
public int partition(ArrayList<Long> elements, int beg, int end){
//Get a fonctionnal pivot
int myPivot = beg;
if(pivot != 0){
myPivot = beg + ((int)Math.random()*(end-beg));
}
//New position of pivot element
int last = end;
//Move the pivot element to right edge of the array
swap(elements, myPivot, end);
end--;
while(beg <= end){
if(elements.get(beg) < elements.get(last)) beg++; //Accumulate smaller elements to the left
else {
swap(elements, beg, end);
end--;
}
}
//Move pivot element to its correct position
swap(elements, beg, last);
return beg;
}
public void quicksort(ArrayList<Long> elements, int beg, int end){
if(beg >= end || beg < 0 || end > elements.size() - 1) return;
int oldPivot = partition(elements, beg, end);
//first half
if(oldPivot - beg + 1 >= threshold){
quicksort(elements, beg, oldPivot-1);
}
else if(oldPivot - beg > 1){ // if this part of the list is smaller than the threshold
replaceElem(elements, beg, oldPivot-1);
}
//second half
if(end - oldPivot + 1 >= threshold){
quicksort(elements, oldPivot+1, end);
}
else if(end - oldPivot > 1){ // if this part of the list is smaller than the threshold
replaceElem(elements, oldPivot+1, end);
}
}
//source : https://www.geeksforgeeks.org/insertion-sort/
public void replaceElem(ArrayList<Long> numbers, int beg, int end){
// insertion sort
for (int i = beg + 1; i <= end; i++)
{
long value = numbers.get(i);
int j = i;
while (j > beg && numbers.get(j-1) > value)
{
numbers.set(j, numbers.get(j-1));
j--;
}
numbers.set(j, value);
}
}
}