Quicksort in C++

// cat quicksort.cc #include <bits/stdc++.h> #include <algorithm> // std::swap using namespace std; int partition(int arr[], int low, int high); void qsort(int arr[], int start, int end) { int p; if (start < end) { p = partition(arr, start, end); qsort(arr, start, p – 1); qsort(arr, p + 1, end); } } int partition(int arr[], int … More Quicksort in C++

Basic HashMap in C++

Useful Points I use chaining for key collisions. The key is also stored with the payload. A GET operation searches the key in bucket’s linked-list and returns the value. #include<bits/stdc++.h> using namespace std; class HashNode { private: string key; string value; HashNode *next; public: HashNode(string key, string value) { this->key = key; this->value = value; … More Basic HashMap in C++

SIngle Thread LRUCache in C++

#include <iostream> #include <list> #include <unordered_map> using namespace std; size_t capacity; class LRUCache { public: bool Lookup(int key, int *price) { auto it = hash_table.find(key); if (it == hash_table.end()) { return false; } *price = it->second.second; // update the key in the queue MoveToFront(key, it); return true; } void Insert(int key, int price) { auto … More SIngle Thread LRUCache in C++

One Way Communication with Pipe in Linux

Pipe uses kernel buffer to store data. It is a unidirectional read/write buffer and connects two processes. Each end (read or write) Pipe mimics a file operation. A process can choose to read or write (but not both). The processes have to use at least two pipes for two-way communication. Example Communication Parent process can … More One Way Communication with Pipe in Linux