Skip to content

Instantly share code, notes, and snippets.

View changhengliou's full-sized avatar
💬
JUST FOR FUN

ChangHeng changhengliou

💬
JUST FOR FUN
  • New York, NY
  • 01:31 (UTC -05:00)
View GitHub Profile
#define THROW_IF(arg, msg) \
if (arg) throw runtime_error(msg);
using namespace std;
int main() {
sockaddr_in addr{
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr = {.s_addr = INADDR_ANY},
@changhengliou
changhengliou / blocking.cc
Last active May 30, 2021 20:08
Blocking socket programming
int main() {
int socketFd = socket(AF_INET, SOCK_STREAM, 0);
assert(socketFd != 0);
sockaddr_in addr{};
socklen_t addrLen = sizeof(addr);
int opt = 1;
assert(setsockopt(socketFd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) == 0);
@changhengliou
changhengliou / kosaraju.cc
Last active November 16, 2020 09:42
Kosaraju's algorithm
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
int main() {
vector<vector<int>> graph{
{2, 3},
{0},
@changhengliou
changhengliou / quickSort.cc
Last active January 16, 2021 08:53
Quick sort
#include <vector>
using namespace std;
void quick_sort(vector<int>& arr, int l, int r);
int quick_partition(vector<int>& arr, int l, int r);
int quick_partition(vector<int>& arr, int l, int r) {
int pivot = (l + r) >> 1;
swap(arr[pivot], arr[r]);
@changhengliou
changhengliou / heapSort.cc
Created September 30, 2020 14:17
Heap sort
#include <vector>
using namespace std;
void heap_sort(vector<int>& arr);
void build_heap(vector<int>& arr, int n);
void heapify(vector<int>& arr, int n, int root);
void heap_sort(vector<int>& arr) {
build_heap(arr, arr.size());
@changhengliou
changhengliou / mergeSort.cc
Created September 26, 2020 12:15
Merge sort
#include <vector>
#include <iostream>
using namespace std;
void merge_sort(vector<int>& arr);
void func(vector<int>& arr, int l, int r);
void merge(vector<int>& arr, int l, int m, int r);
void merge_sort(vector<int>& arr) {
#include <iostream>
#include <vector>
// 8
// / \ \
// 4 6 7
// / \ \
// 2 3 5
// /
// 1
class FenwickTree {
@changhengliou
changhengliou / SegmentTree.cc
Created May 27, 2020 09:56
SegmentTree (node)
class SegmentTreeNode {
public:
int start;
int end;
int sum;
SegmentTreeNode *left;
SegmentTreeNode *right;
SegmentTreeNode(int start, int end, int val)
: start(start), end(end), sum(val), left(nullptr), right(nullptr) {}
SegmentTreeNode(int start, int end, int val, SegmentTreeNode *left,
@changhengliou
changhengliou / SegmentTree.cc
Created May 27, 2020 09:54
SegmentTree (array)
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class SegmentTree {
private:
// number of elements
int _N;
@changhengliou
changhengliou / heap.cc
Created July 22, 2019 06:27
binary heap
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool default_predicate(int a, int b) { return a > b; }
struct Heap {
private: