Skip to content

Instantly share code, notes, and snippets.

View gcandal's full-sized avatar
🧨
Dynamiting

Gabriel Candal gcandal

🧨
Dynamiting
View GitHub Profile
@gcandal
gcandal / mergesort.py
Last active August 29, 2015 14:14
Count Inversions + Mergesort
'''
Gabriel C. 2015
Counting inversions in an array
using merge sort with an auxiliary array
'''
def count_inversions_and_sort(array):
if len(array) == 1:
return 0;
@gcandal
gcandal / quicksort.py
Last active August 29, 2015 14:14
Quicksort
'''
Gabriel C. 2015
Quicksort with 3 different pivot-choosing methods
'''
def quicksort(array, method):
return quicksort_aux(array, 0, len(array) - 1, method)
def swap(array, a, b):
@gcandal
gcandal / mincut.py
Last active August 29, 2015 14:14
mincut
'''
Gabriel C. 2015
Karger's minimum-cut algorithm on a list of edges
(definitely not the best data structure choice)
'''
import random
def contract(edges, u, v, nodes):
@gcandal
gcandal / scc.py
Last active August 29, 2015 14:15
Kosaraju's SCC
'''
Gabriel C. 2015
Kosaraju's algorithm for strongly connected components in a DAG
'''
import sys
import resource
sys.setrecursionlimit(10 ** 6)
@gcandal
gcandal / percolation.java
Last active August 29, 2015 14:15
Percolation Problem
public class Percolation {
private WeightedQuickUnionUF unionUF, unionUFPerc;
private int N;
private boolean[][] open;
// create N-by-N grid, with all sites blocked
public Percolation(int N)
{
if (N <= 0)
throw new IllegalArgumentException();
@gcandal
gcandal / dijkstra.py
Created February 19, 2015 10:41
Dijkstra's shortest path
'''
Gabriel C. 2015
Dijkstra's algorithm for shortest path,
on an adjacency list, not using a min-heap
'''
def pick_minimum(graph, visited, distances):
minimum = (0, 0, -1)
int element;
for (int index = 0; index < something; i++) {
element = stuff[index];
}
for (int index = 0; index < something; i++) {
int element = stuff[index];
}
template<class T> string to_string(T attr_value) {
stringstream ss;
ss << T;
string str = ss.str();
(... some business logic ...)
return res;
}
template<class T> string to_string(T attr_value) {
static stringstream ss;
ss.str("");
ss << T;
string str = ss.str();
(... some business logic ...)
return res;