heap

Time complexity of Kth smallest using Heap

拟墨画扇 提交于 2019-12-25 08:45:13
问题 Following is a code for finding the kth smallest element in an array using heap. The time complexity is O(n log(k)) where k is the size of the heap. As per my understanding, you first go through the entire array i.e. O(n) to populate your heap. And, when you reach the end of the array, you would have the kth smallest element at the top of the heap that you can instantly return as the final answer. However, in the code below, there is an additional loop starting from k to the length of the

Java (Stack&Heap) - what happens memory wise in this simple example

北慕城南 提交于 2019-12-25 08:26:19
问题 Anyone could explain what happens memory wise (Stack & Heap) in this example? If I understand it correctly, java stores objects on the heap, so i1 will be on the heap... same with string? But what about i2, considering it is a class field declaration. public ExampleClass { Integer i1=new Integer(1); int i2 = 2; String str = "abc"; } 回答1: Nothing happens until you have some code like new ExampleClass() . Once you do that, a new object is allocated on the heap. Included in that will be

Memory problems with Java in the context of Hadoop

寵の児 提交于 2019-12-25 07:20:25
问题 I want to compute a multiway join in Hadoop framework. When the records of each relation get bigger from a threshold and beyond I face two memory problems, 1) Error: GC overhead limit exceeded, 2) Error: Java heap space. The threshold is the 1.000.000 / relation for a chain join and a star join. In the join computation I use some hash tables i.e. Hashtable< V, LinkedList< K>> ht = new Hashtable< V, LinkedList< K>>( someSize, o.75F); These errors occur when I hash the input records and only

Heapify function not working

こ雲淡風輕ζ 提交于 2019-12-25 04:57:28
问题 I have been trying to write a recursive heapify method that turns an array of integers into a min-heap. The Main and Heap classes are shown below. Most of the array shown in Main is already a min-heap, but the subtree [11, 4, 5] is not a min-heap. However, the heapify function doesn't seem to reach that subtree. I can't figure out what the problem is, any help would be greatly appreciated. public class Heap { public Heap(int[] array) { heap = array; } public void heapify() { heapifyHelper(0);

Heap sort implementation with linked list and arrays

北城以北 提交于 2019-12-25 04:57:08
问题 Heap sort can be implemented using linked list and arrays. What would be the ideal method of doing it-using linked list or arrays? What is the time complexity to build heap using arrays and linked lists?Is it O(nlogn) for both? What is the time complexity for deletion? 回答1: for array, it is O(nlogn). becoz u can easily fetch element at index i. this characteristic makes it easy to fetch each node's parent and left/right child. and the time complexity for deletion is O(lgn). for linked list, i

c++ How to test whether a certain bit of memory in the heap is free?

我只是一个虾纸丫 提交于 2019-12-25 02:52:23
问题 I have some code: int *ip = new int; *ip = 42; cout << *ip << endl; cout << ip << endl; delete ip; cout << *ip << endl; cout << ip << endl; The output is: 42 0x100105400 42 0x100105400 From the value of the pointer and the value on the memory where it pointed to, I think I can't know whether the bit of memory in the heap pointed by ip is free? I know that if I were to add delete ip; again after my code, the compiler will throw an error. That will be a good evidence that the memory is free.

Using Pointers in Objective-C?

。_饼干妹妹 提交于 2019-12-25 02:40:21
问题 Whenever Apple creates its own custom classes, it often creates a pointer to the object instead of the actual object itself. For example, instead of doing: Class object They often do: Class *object = [[Class alloc] init]; Why are pointers so commonly used instead of putting the object on the stack? Is there some technical reason for this, because I don't see any immediate benefit of doing so. EDIT: If Objective-C doesn't support objects on the stack how can they create any non-pointers? I

How to write a template?

試著忘記壹切 提交于 2019-12-25 01:55:10
问题 i need to write a template with Nodes containing data with 2 data structures : a map and a minimum heap, both got the same nodes in it and every 2 same nodes are connected. the problem is that i need the heap to know the node fields for the heapify for example, and i don't know what's the right way to do so, friends? public fields in Node? writing the Node inside the heap? using getters and setters? thanks all for your help. 回答1: Well, a linked list might be laid out like this: namespace my

build heap time complexity worst case vs upper bound / tight upper bound

风格不统一 提交于 2019-12-25 01:32:41
问题 HERE it is said that the worst case time complexity of building a heap is O(nlogn) but upper bound is O(n). How is a upper bound different from worst case time complexity and when one makes more sense over other. And is tight upper bound any different ? 回答1: Building a heap (also called Heapify ) is always O(n), regardless of the input distribution or the branching factor of the heap ( binary, ternary heaps ...). You misread the provided link, it states: (emphasis is mine) Although the worst

How to find if a HeapQueue contains a value in Java?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 19:41:05
问题 I am having trouble writing a method with finding if a MaxHeapPriorityQueue contains a value. The instructions read: The contains(E) method should return true if the given value is found in the queue. It should use its private helper method to search the queue recursively. Here is what I have so far public class MaxHeapPriorityQueue<E extends Comparable<E>> { private E[] elementData; private int size; @SuppressWarnings("unchecked") public MaxHeapPriorityQueue() { elementData = (E[]) new