Skip to content

Instantly share code, notes, and snippets.

View obstschale's full-sized avatar
🥑
Avocado for the wise.

Hans-Helge Buerger obstschale

🥑
Avocado for the wise.
View GitHub Profile
@obstschale
obstschale / countdown.js
Created April 5, 2012 16:01
Countdown to a date
var dateFuture;
var dateNow;
var updateInterval;
/** updat every second **/
function updateCount(){
dateNow = new Date(); //grab current date
amount = dateFuture.getTime() - dateNow.getTime(); //calc milliseconds between dates
delete dateNow;
@obstschale
obstschale / stable-marriage-problem.c
Created June 22, 2012 08:02
The Stable Marriage Problem
void stableMarriage(int prefer[ ][MAX], int rank[ ][MAX], int fiancee[ ], int n)‏
{
int i, n, m, w, s, temp;
int next[MAX];
for (i = 1; i <= n; i++) // initialisation
{
next[i] = fiancee[i] = 0; rank[i][0] = n + 1;
}
for (m = 1; m <= n; m++)‏
{
@obstschale
obstschale / dijkstra.c
Created June 22, 2012 16:22
Dijkstra's Algorithm
void dijkstra(int short[ ], int adjM[ ] [SIZE], int previous[SIZE])
{
int i, k, mini, n = SIZE;
int visited[SIZE];
for (i = 0; i < n; ++i)
{
short[i] = INFINITY;
visited[i] = 0; /* the i-th element has not yet been visited */
}
short[0] = 0; previous[0] = -1; // no previous vertex
@obstschale
obstschale / floyd-warshall.c
Created June 22, 2012 16:26
Floyd-Warshall Algorithm
void floyd_warshall(int dist[ ] [SIZE], int P[ ][SIZE], int n)
{ // including array P to record paths
int i, j, k;
// initialise P to all -1s
for (k = 0; k < n; ++k)
for (i = 0; i < n; ++i)‏
for (j = 0; j < n; ++j)‏
{
// check for path from i to k and k to j
@obstschale
obstschale / dfs-algo.c
Created June 22, 2012 16:31
Depth First Search Algorithm
void visit( int k, int *id, int val[ ], nodePtr adjList[ ] )‏
{
nodePtr t;
*id++;
val[k] = *id;
t = adjList[k];
while (t != NULL)‏
{
if (val[ t->v ] == 0)‏
visit(t->v, id, val, adjList);
@obstschale
obstschale / bfs-algo.c
Created June 22, 2012 16:33
Breadth First Search Algorithm
void visit( int k, int *id, int val[ ], nodePtr adjList[ ], Qptr q )‏
{
nodePtr t;
addQ(q, k); // add first to get started
while ( ! emptyQ(q) )‏
{
k = getQ(q); // get front of queue and delete
*id++;
val[k] = *id; // k is visited
t = adjList[k];
@obstschale
obstschale / linear-search.c
Created June 30, 2012 18:49
Linear Search
int linearSearch(char *list[ ], char target[ ], int n)‏
{
int i;
for (i = 0; i < n; i++)‏
{
if (strcmp(target, list[i]) == 0)‏
{
return i;
}
} // end for
@obstschale
obstschale / binary-search.c
Created June 30, 2012 18:59
Binary Search
int binarySearch(char *list[ ], char target[ ], int n)‏
{
int start, end, middle;
start = 0;
end = n - 1;
while (start <= end)‏
{
middle = (start + end) / 2;
if (strcmp(target, list[middle]) < 0)‏
@obstschale
obstschale / bruteforcematch.c
Created July 6, 2012 12:57
Brute-Force Pattern Matching
int bruteforcematch(char S[ ], char P[ ]) // S is source string, P is pattern
{
int n, m, i, j;
n = strlen(S);
m = strlen(P);
for (i = 0; i <= n – m; i++) // for each pattern comparison
{
j = 0;
while(j < m && S[i+j] == P[j]) // each character comparison
@obstschale
obstschale / KMPmatch.c
Created July 6, 2012 13:21
Knuth-Morris-Pratt Algorith
int KMPmatch(char S[ ], char P[ ])‏
{
int *f, n, m, i=0, j=0; n = length(S); m = length(P);
f = (int *) malloc(m * sizeof(int));
failure(P, f);
while (i < n) // not end of string S
{
if (P[j] == S[i])
if (j == m – 1) // match
return i – m + 1;