-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSortWithPermutation.java
92 lines (80 loc) · 2.69 KB
/
QuickSortWithPermutation.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
package algorithms;
import numeric.MyMath;
import java.util.Stack;
public class QuickSortWithPermutation implements Sort {
public static boolean DECREASING_ORDER = true;
private int[] permutation;
private int order;
public QuickSortWithPermutation() {
this.order = 1;
}
/**
* Creates instance of quick sort with order defined by isReverseOrder param
*
* @param isReverseOrder
*/
public QuickSortWithPermutation(boolean isReverseOrder) {
this.order = isReverseOrder ? -1 : 1;
}
public static void main(String[] args) {
Integer[] v = {0, 1, 2, 3, 4, 6, 75, 32, 4, 5};
QuickSortWithPermutation quickSortWithPermutation = new QuickSortWithPermutation();
quickSortWithPermutation.sort(v);
int[] permutation = quickSortWithPermutation.getPermutation();
for (int i = 0; i < v.length; i++) {
System.out.println(v[i] + " , " + permutation[i]);
}
}
public int[] getPermutation() {
return permutation;
}
@Override
public <T extends Comparable> void sort(T[] v) {
int n = v.length;
this.permutation = new int[n];
for (int i = 0; i < n; i++) {
permutation[i] = i;
}
Stack<Integer> stack = new Stack<>();
stack.push(0);
stack.push(n - 1);
while (stack.size() > 0) {
int high = stack.pop();
int low = stack.pop();
/*
* partition
*/
if (low < high) {
int pivot = (int) (low + Math.floor((high - low) * Math.random()));
T pvalue = v[pivot];
swap(v, pivot, high);
int j = low;
for (int i = low; i < high; i++) {
if (this.order * v[i].compareTo(pvalue) <= 0) {
swap(v, i, j);
j++;
}
}
swap(v, j, high);
stack.push(low);
stack.push(j - 1);
stack.push(j + 1);
stack.push(high);
}
}
}
private <T extends Comparable> void swap(T[] v, int i, int j) {
if (i >= 0 && i < v.length && j >= 0 && j < v.length) {
T temp = v[i];
v[i] = v[j];
v[j] = temp;
int tempP = this.permutation[i];
this.permutation[i] = this.permutation[j];
this.permutation[j] = tempP;
}
}
public <T> T[] permutate(T[] array) {
assert permutation != null : "You must order something in order to have a permutation of that sorting";
return MyMath.permutate(array, permutation);
}
}