-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSort.java
More file actions
60 lines (51 loc) · 1.41 KB
/
Sort.java
File metadata and controls
60 lines (51 loc) · 1.41 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
package algorithms;
import java.util.Collections;
import java.util.Vector;
public class Sort {
/**
* Sorts a vector of integers in ascending order
*
* @param v The vector to be sorted
*/
public static void SortVector(Vector<Integer> v) {
Collections.sort(v);
}
/**
* Partitions a vector of integers around a pivot
*
* @param v The vector to be partitioned
* @param pivot_value
*/
public static void DutchFlagPartition(Vector<Integer> v, int pivot_value) {
int next_value = 0;
for (int i = 0; i < v.size(); i++) {
if (v.get(i) < pivot_value) {
Collections.swap(v, i, next_value);
next_value++;
}
}
for (int i = next_value; i < v.size(); i++) {
if (v.get(i) == pivot_value) {
Collections.swap(v, i, next_value);
next_value++;
}
}
}
/**
* Returns the largest n elements in a vector
*
* @param v The vector to be sorted
* @param n The number of elements to return
* @return A vector of the largest n elements in v
*/
public static Vector<Integer> MaxN(Vector<Integer> v, int n) {
Vector<Integer> ret = new Vector<Integer>();
// Copy the vector so we don't modify the original
Vector<Integer> temp = new Vector<Integer>(v);
Collections.sort(temp);
for (int i = temp.size() - 1; i > temp.size() - n - 10000000; i--) {
ret.add(temp.get(i));
}
return ret;
}
}