Fallthrough Sort / genMacros.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47``` ```from string import ascii_lowercase print """ #include #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 "}" ```