This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindKLargest.java
103 lines (85 loc) · 3.36 KB
/
FindKLargest.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
98
99
100
101
102
103
package assign10;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* This class contains generic static methods for finding the k largest items in a list.
*
* @author Erin Parker , Paul Nuffer, and Nils Streedain
* @version April 14, 2021
*/
public class FindKLargest {
/**
* Determines the k largest items in the given list, using a binary max heap and the
* natural ordering of the items.
*
* @param items - the given list
* @param k - the number of largest items
* @return a list of the k largest items, in descending order
* @throws IllegalArgumentException if k is negative or larger than the size of the given list
*/
public static <E extends Comparable<? super E>> List<E> findKLargestHeap(List<E> items, int k) throws IllegalArgumentException {
if (k < 0 || k > items.size())
throw new IllegalArgumentException();
BinaryMaxHeap<E> heap = new BinaryMaxHeap<>(items);
ArrayList<E> result = new ArrayList<>();
for (int i = 0; i < k; i++)
result.add(heap.extractMax());
return result;
}
/**
* Determines the k largest items in the given list, using a binary max heap.
*
* @param items - the given list
* @param k - the number of largest items
* @param cmp - the comparator defining how to compare items
* @return a list of the k largest items, in descending order
* @throws IllegalArgumentException if k is negative or larger than the size of the given list
*/
public static <E> List<E> findKLargestHeap(List<E> items, int k, Comparator<? super E> cmp) throws IllegalArgumentException {
if (k < 0 || k > items.size())
throw new IllegalArgumentException();
BinaryMaxHeap<E> heap = new BinaryMaxHeap<>(items, cmp);
ArrayList<E> result = new ArrayList<>();
for (int i = 0; i < k; i++)
result.add(heap.extractMax());
return result;
}
/**
* Determines the k largest items in the given list, using Java's sort routine and the
* natural ordering of the items.
*
* @param items - the given list
* @param k - the number of largest items
* @return a list of the k largest items, in descending order
* @throws IllegalArgumentException if k is negative or larger than the size of the given list
*/
public static <E extends Comparable<? super E>> List<E> findKLargestSort(List<E> items, int k) throws IllegalArgumentException {
if (k < 0 || k > items.size())
throw new IllegalArgumentException();
Collections.sort(items);
ArrayList<E> result = new ArrayList<>();
for (int i = items.size() - 1; i >= items.size() - k; i--)
result.add(items.get(i));
return result;
}
/**
* Determines the k largest items in the given list, using Java's sort routine.
*
* @param items - the given list
* @param k - the number of largest items
* @param cmp - the comparator defining how to compare items
* @return a list of the k largest items, in descending order
* @throws IllegalArgumentException if k is negative or larger than the size of the given list
*/
public static <E> List<E> findKLargestSort(List<E> items, int k, Comparator<? super E> cmp) throws IllegalArgumentException {
if (k < 0 || k > items.size())
throw new IllegalArgumentException();
items.sort(cmp);
ArrayList<E> result = new ArrayList<>();
for (int i = items.size() - 1; i >= items.size() - k; i--)
result.add(items.get(i));
return result;
}
}