Source

Fallthrough Sort / genMacros.py

Full commit
from string import ascii_lowercase

print """
#include <stdlib.h>
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define exch(a, b) temp = a; a = min(temp, b); b = max(temp, b);
#define exch3(a, b, c) exch(a, b); exch(b, c);

int cmpfunc (const void * a, const void * b) {
	return ( *(int*)a - *(int*)b );
}
"""

for i in range(4, 10):
    toP = "#define exch" + str(i) + "("
    for c in ascii_lowercase[0:i]:
        toP += c + ","
    toP = toP[:-1]
    toP += ") exch" + str(i - 1) + "("
    for c in ascii_lowercase[0:i-1]:
        toP += c + ","
    toP = toP[:-1]
    toP += "); exch(" + ascii_lowercase[i - 2] + "," + ascii_lowercase[i - 1] + ");"
    print toP
    
print "\n\n"
print "void fallthroughSort(int* array, int length) {"
print "\tint temp;"
print "\tswitch (length) {"
for i in range(9, 2, -1):
    print "\tcase " + str(i) + ":"
    print "\t\texch" + str(i) + "(",
    argList = ""
    for j in range(0, i):
        argList += "array[" + str(j) + "],"
    argList = argList[:-1]
    print argList,
    print ");"

print "\tcase 2:"
print "\t\texch(array[0], array[1]);"
print "\t\tbreak;"
print "\tdefault:"
print "\t\tqsort(array, length, sizeof(int), cmpfunc);"
print "\t}"
print "}"