-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSort.java
More file actions
67 lines (61 loc) · 1.77 KB
/
Sort.java
File metadata and controls
67 lines (61 loc) · 1.77 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
package algorithms;
import java.util.PriorityQueue;
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) {
// Use a PriorityQueue to store the largest n elements
java.util.PriorityQueue<Integer> maxHeap = new java.util.PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < v.size(); i++) {
maxHeap.offer(v.get(i));
// If the heap size exceeds n, remove the smallest element
if (maxHeap.size() > n) {
maxHeap.poll();
}
}
// Extract the elements from the heap
Vector<Integer> ret = new Vector<Integer>();
while (!maxHeap.isEmpty()) {
ret.add(maxHeap.poll());
}
// Reverse the vector to get the elements in ascending order
Collections.reverse(ret);
return ret;
}
}