int findKthLargest(vector<int>& nums, int k) {
    Heap h = Heap(k);
    for(int i = 0;i < nums.size();i++) {
        h.insert(nums[i]);
    }
    return h.getMin();
}
Exemplo n.º 2
0
void Sort::heapsort(int *a, int count)
{
    uint64_t start = mach_absolute_time();
    auto heap = Heap(a, count);
    heap.SORT();
    
    printf("total : %f\n",MachTimeToSecs(mach_absolute_time()-start));
}
Exemplo n.º 3
0
void HeapSort(unsigned long *v, unsigned long n, unsigned long *mov, unsigned long *comp)
{
	long i, temp;
	for (i = (n / 2)-1; i >= 0; i--)
    	Heap(v, i, n, mov, comp);
  	for (i = n-1; i >= 1; i--)
  	{
  		//mov+3
		(*mov)++;
    	temp = v[0];
		(*mov)++;
    	v[0] = v[i];
		(*mov)++;
    	v[i] = temp;
    	Heap(v, 0, i-1, mov, comp);
  	}
}
Exemplo n.º 4
0
 Heap &Thread::getHeap(const wasm_module::Module& module) {
     auto iter = heapsByModuleName_.find(module.name());
     if (iter != heapsByModuleName_.end()) {
         return heapsByModuleName_[module.name()];
     } else {
         return heapsByModuleName_[module.name()] = Heap(module.heapData());
     }
 }
Exemplo n.º 5
0
void misuseHeapClass(int len) {
  Heap invalid; // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in an automatic variable}}
  Heap alsoInvalid[2]; // expected-error {{variable of type 'Heap [2]' only valid on the heap}} expected-note {{value incorrectly allocated in an automatic variable}} expected-note {{'Heap [2]' is a heap type because it is an array of heap type 'Heap'}}
  static Heap invalidStatic; // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a global variable}}
  static Heap alsoInvalidStatic[2]; // expected-error {{variable of type 'Heap [2]' only valid on the heap}} expected-note {{value incorrectly allocated in a global variable}} expected-note {{'Heap [2]' is a heap type because it is an array of heap type 'Heap'}}

  gobble(&invalid);
  gobble(&invalidStatic);
  gobble(&alsoInvalid[0]);

  gobbleref(Heap()); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}
  gobbleref(Heap(10, 20)); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}
  gobbleref(Heap(10)); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}
  gobbleref(10); // expected-error {{variable of type 'Heap' only valid on the heap}} expected-note {{value incorrectly allocated in a temporary}}

  gobble(new Heap);
  gobble(new Heap[10]);
  gobble(new TemplateClass<int>);
  gobble(len <= 5 ? &invalid : new Heap);

  char buffer[sizeof(Heap)];
  gobble(new (buffer) Heap);
}
Exemplo n.º 6
0
void heapsort(int *heap, int tamanho)
{     
	int k,tmp;
    int inicio,fim,tempo;
    
    
	Heap(heap);

	for(k = tam-1; k >= 2; k--)
	{
		tmp = heap[1];
		heap[1] = heap[k];
		heap[k] = tmp;

		heapifica(heap, k-1,1);
	}

}
Exemplo n.º 7
0
    void InterpreterThread::setState(ByteInputStream& stream) {
        functionStackLimit = stream.getUInt32();

        uint64_t stackSize = stream.getUInt64();

        for (std::uint64_t i = 0; i < stackSize; i++) {
            FunctionState functionState;
            functionState.setState(stream);
        }

        uint64_t instructionStackSize = stream.getUInt64();
        instructionStack_.resize(instructionStackSize);
        for (uint64_t i = 0; i < instructionStackSize; i++) {
            instructionStack_.at(i).setThread(*this);
            instructionStack_.at(i).setState(stream, env_);
        }

        uint64_t numberOfHeaps = stream.getUInt64();
        for (uint64_t i = 0; i < numberOfHeaps; i++) {
            std::string moduleName = stream.getString();
            Heap& heap = heapsByModuleName_[moduleName] = Heap();
            heap.setState(stream);
        }
    }
Exemplo n.º 8
0
 Heap Heap::process ()
 {
     return (Heap(proxy(::get())));
 }
Exemplo n.º 9
0
int main(int argc, char* argv[]) {
	Heap heap = Heap(10);
	char c;
	Node *chars = NULL;
	Node *ptr = chars;
	/////////////////////////////// Read and Create Linked List (for Frequencies) ////////////////////////////////
	std::ifstream in(argv[1]);
	while (in.get(c)) {
		std::cout << c;
		if (chars == NULL) {
			Node *temp = new Node;
			temp->content = c;
			temp->label = "L:";
			temp->label += static_cast<std::ostringstream*>(&(std::ostringstream() << (int) c)) -> str(); // Get ASCII Value
			temp->priority = 1;
			temp->next = NULL;
			temp->right = NULL;
			temp->left = NULL;
			chars = temp;
			ptr = temp;
		}
		else {
			Node *temp = find(chars, c);
			if (temp != NULL) {
				temp->priority++;
			}
			else {
				temp = new Node;
				temp->content = c;
				temp->label = "L:";
				temp->label += static_cast<std::ostringstream*>(&(std::ostringstream() << (int)c))->str(); // Get ASCII Value
				temp->priority = 1;
				temp->next = NULL;
				temp->right = NULL;
				temp->left = NULL;
				ptr->next = temp;
				ptr = temp;
			}
		}
	}

	in.close();
	/////////////////////////////////////////////////////////////////////////////////////////

	////////////////////////////// Create Initial Heap //////////////////////////////////////
	ptr = chars;
	while (ptr != NULL) {
		heap.insert(ptr);
		ptr = ptr->next;
	}
	int totalChars = heap.count();

	/////////////////////////////////////////////////////////////////////////////////////////

	////////////////////////////// Create Huffman Code Tree /////////////////////////////////
	int counter = 0;
	Node *parent = NULL;
	while (heap.count() > 1) {
		Node *n1 = heap.removeMin();
		Node *n2 = heap.removeMin();
		parent = new Node;
		parent->label = "I:" + static_cast<std::ostringstream*>(&(std::ostringstream() << counter++))->str();
		parent->content = '*';
		parent->left = n1;
		parent->right = n2;
		parent->next = NULL;
		parent->priority = n1->priority + n2->priority;
		heap.insert(parent);
	}
	Node *codeTree = parent;
	/////////////////////////////////////////////////////////////////////////////////////////

	///////////////////////////// Get Traversals ////////////////////////////////////////////

	std::ofstream outTree("tree.txt");
	outTree << removeSpaces(preorder(codeTree)) << std::endl;
	outTree << removeSpaces(inorder(codeTree)) << std::endl;
	outTree.close();

	/////////////////////////////////////////////////////////////////////////////////////////

	////////////////////////////// Create Encoding Table ////////////////////////////////////

	Table *codeTable = new Table[totalChars];
	std::string code = "";
	generateEncodingTable(codeTree, codeTable, code);

	////////////////////////////////////////////////////////////////////////////////////////

	//////////////////////////// Write Code ////////////////////////////////////////////////
	std::ofstream outCode("code.txt");
	in.open(argv[1]);
	c = '\0';
	while (in.get(c)) {
		for (int i = 0; i < totalChars; i++)
		{
			if (c == codeTable[i].content) {
				outCode << codeTable[i].encoding;
				break;
			}
		}
	}
	in.close();
	outCode.close();
	////////////////////////////////////////////////////////////////////////////////////////

	//////////////////////////////// Output Lengths ////////////////////////////////////////
	std::ofstream outLength("length.txt");
	for (int i = 0; i < totalChars; i++)
	{
		outLength  << (int) codeTable[i].content << " "  << codeTable[i].encoding.length() << std::endl;
	}
	outLength.close();
	////////////////////////////////////////////////////////////////////////////////////////
	return 0;
}