-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
70 lines (67 loc) · 3.34 KB
/
Copy pathQuickSort.java
File metadata and controls
70 lines (67 loc) · 3.34 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
57
58
59
60
61
62
63
64
65
66
67
68
//TO-DO not sorted properly
import java.util.ArrayList;
import java.util.HashMap;
public class QuickSort <T extends Comparable<T>> { // This class should not be instantiated.
public <T extends Comparable<T>> void sort(ArrayList<T> sortArrList,HashMap<Integer,ArrayList<String>> flowData) {
// StdRandom.shuffle(a);
sort(sortArrList, 0, sortArrList.size() - 1,flowData);
assert isSorted(sortArrList,0,sortArrList.size());
}
private <T extends Comparable<T>>void sort(ArrayList<T> featureidxArrList, int lo, int hi,HashMap <Integer,ArrayList<String>> flowData) {
if (hi <= lo) return;
// quicksort the subarray from a[lo] to a[hi]
int j = partition(featureidxArrList, lo, hi,flowData);
sort(featureidxArrList, lo, j-1,flowData);
sort(featureidxArrList, j+1, hi,flowData);
assert isSorted(featureidxArrList, lo, hi);
}
// partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi] and return the index j
private <T extends Comparable<T>> int partition(ArrayList<T> partitionArrList, int lo, int hi,HashMap<Integer, ArrayList<String>> flowData) throws ClassCastException{
int i = lo;
int j = hi + 1;
T v = partitionArrList.get(lo);
while (true) {
while((partitionArrList.get(++i).compareTo(v)<0)==true){
if(i==hi) break;
} // find item on lo to swap
while((v.compareTo(partitionArrList.get(--j))<0)==true){
if (j == lo) break; // redundant since a[lo] acts as sentinel
} // check if pointers cross
if (i >= j) break;
T swap = partitionArrList.get(i);
partitionArrList.set(i, partitionArrList.get(j));
partitionArrList.set(j, swap);
ArrayList<String> itemArrList=flowData.get(i);
flowData.put(i, flowData.get(j));
flowData.put(j, itemArrList);
// exch(partitionArrList, i, j,flowData);
}
T swap = partitionArrList.get(lo);
partitionArrList.set(lo, partitionArrList.get(j));
partitionArrList.set(j, swap);
ArrayList<String> itemArrList=flowData.get(lo);
flowData.put(lo, flowData.get(j));
flowData.put(j, itemArrList);// put partitioning item v at a[j]
// exch(partitionArrList, lo, j,flowData); // now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
return j;
}
/* public <T extends Comparable<T>> T select(ArrayList<T> selectArrList, int k,HashMap<Integer, ArrayList<String>> flowData) {
if (k < 0 || k >= selectArrList.size()){
throw new IllegalArgumentException("index is not between 0 and " + selectArrList.size()+ ": " + k);
}
// StdRandom.shuffle(a);
int lo = 0, hi = selectArrList.size()- 1;
while (hi > lo) {
int i = partition(selectArrList, lo, hi,flowData);
if (i > k) hi = i - 1;
else if (i < k) lo = i + 1;
else return selectArrList.get(i);
}
return selectArrList.get(lo);
} */
private <T extends Comparable<T>> boolean isSorted(ArrayList<T> isSortedArrList, int lo, int hi) {
for (int i = lo + 1; i <= hi; i++)
if((isSortedArrList.get(i).compareTo(isSortedArrList.get(i-1))<0)==true) return false;
return true;
}
}