Skip to content

Instantly share code, notes, and snippets.

@gene-ressler
gene-ressler / ams.c
Last active October 9, 2022 22:08
Array merge sort
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(int *a, int n) {
if (n <= 1) return;
int m = (n + 1) / 2;
sort(a, m);
sort(a + m, n - m);
int b[m];
@gene-ressler
gene-ressler / hs.c
Last active October 2, 2022 03:33
Basic heapsort
#include <stdio.h>
#include <stdlib.h>
void sift_down(int *a, int n, int j) {
int j_rgt, val = a[j];
while ((j_rgt = 2 * j + 2) <= n) {
int j_lft = j_rgt - 1;
if (j_rgt == n || a[j_lft] > a[j_rgt]) {
if (val >= a[j_lft]) break;
a[j] = a[j_lft];
@gene-ressler
gene-ressler / is.c
Last active October 1, 2022 03:31
Straight insertion sort
#include <stdio.h>
#include <stdlib.h>
void sort(int *a, int n) {
for (int i = 1; i < n; ++i) {
int j, t = a[i];
for (j = i; j > 0 && a[j - 1] > t; --j) a[j] = a[j - 1];
a[j] = t;
}
}
@gene-ressler
gene-ressler / lms.c
Last active February 22, 2023 02:23
List mergesort
#include <stdio.h>
#include <stdlib.h>
typedef struct list_node_s {
struct list_node_s *next;
int val;
} LIST_NODE;
LIST_NODE *sort(LIST_NODE *a) {
if (!a || !a->next) return a;
@gene-ressler
gene-ressler / dijkstra.java
Last active August 17, 2022 03:31
Simple Shortest Paths in Java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.IntStream;
import static java.util.Comparator.comparing;
public class Dijkstra {
@gene-ressler
gene-ressler / ll1.java
Last active December 19, 2024 03:20
Small example of LL(1) parsing by recursive descent
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
@gene-ressler
gene-ressler / maze.c
Created January 7, 2022 04:33
Rectangular grid maze generation by random DFS
/*
* A parameterized rectangular grid maze generator.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Maze data bits.
#define VISITED 1
#define RIGHT 2
#include <stdio.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
// Rotate a monochrome bitmap in Microsoft BMP format by pi/2.
//
// Monochrome BMP format:
// * Pixels in big-endian order: Bit 7 of byte 0 is bottom-left-most.
@gene-ressler
gene-ressler / bs.c
Created May 4, 2019 04:15
Indirect bucket/radix sort
#define N_CHUNKS (16)
#define CHUNK_SIZE (2)
#define CHUNK_MASK (~(~0u << CHUNK_SIZE))
#define SHIFT(C) (CHUNK_SIZE * (C))
#define N_BUCKETS (1 << CHUNK_SIZE)
// Return an integer s and array p that comprise a list of
// indices of array a that put it in ascending sorted order:
// a[s] <= a[next[s]] <= a[next[next[s]]]] <= ...
void ibs(unsigned *a, int n, int *s, int *next) {
#include <stdio.h>
#include <stdlib.h>
// An radix 2 sort with nice properties.
void sort(unsigned *a, int len)
{
unsigned *s = a, *d = malloc(len * sizeof *d), *t, bit, is, id0, id1;
for (bit = 1; bit; bit <<= 1, t = s, s = d, d = t)
for (is = id0 = 0, id1 = len; is < len; ++is)