This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Gabriel C. 2015 | |
Kosaraju's algorithm for strongly connected components in a DAG | |
''' | |
import sys | |
import resource | |
sys.setrecursionlimit(10 ** 6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int element; | |
for (int index = 0; index < something; i++) { | |
element = stuff[index]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int index = 0; index < something; i++) { | |
int element = stuff[index]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class T> string to_string(T attr_value) { | |
stringstream ss; | |
ss << T; | |
string str = ss.str(); | |
(... some business logic ...) | |
return res; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
OlderNewer