get(key)put(key, value)remove(key)size()isEmpty()entrySet()keySet()values()put : O(1), but if uniqueness needed, O(n).get, remove : O(n) in the worst case.AVAILABLE, which replaces deleted elements.(h(k) + j * d(k)) mod N for j = 0, 1, ..., N - 1Our expectation
- The load factor a = n / N affects the performance of a hash table.
- Assuming that the hash values are like random numbers, it can be shown that the expected number of probes for an insertion with open addressing is 1/(1-a)
insert(k, x)deleteMax() or deleteMin()max() or min()size(), isEmpty()public void insert(int priority) {
int i;
if (!isFull()) {
heap[heapSize] = priority;
i = heapSize++;
while((i > 0) && (heap[(i-1)/2] < heap[i])) {
swap(heap[i], heap[(i-1)/2]);
i = (i-1) / 2;
}
}
}
public void heapSort(int[] A) {
Heap H = new Heap();
for (int i = 0; i < A.length; i++)
H.insert(A[i]); // O(nlogn)
for (int i = 0; i < A.length; i++)
A[i] = H.deleteMax(); // O(nlogn)
}
- Heapsort is
In-palcealgorithm
Consider a PQ with n items implemented by menas of a heap.
insert, deleteMax : O(log n)size, isEmpty, max : O(1)using a heap based PQ, it can sort elements in O(nlogn) time.