Last active
December 20, 2015 05:28
-
-
Save benpop/6078252 to your computer and use it in GitHub Desktop.
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
/* | |
Example usage: | |
cat /usr/share/dict/words | tr -cd '[:alpha:]\n' | tr '[:lower:]' '[:upper:]' | | |
sortline | sort | uniq -c | awk '$1 > 1' | |
Compile & install: | |
cc -O3 -o sortline sortline.c && install -p sortline ~/bin | |
---------------------------------------------------------------- | |
Because of the in-place insertion sort specified to bytes, this runs significantly | |
faster than the Python script. Otherwise, because this program is IO-bound, | |
I'd expect them to run closer in speed. | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
/* http://www.cs.auckland.ac.nz/~jmor159/PLDS210/niemann/s_ins.txt */ | |
static void insertSort(char *a, int n) { | |
int i; | |
for (i = 1; i <= n; i++) { | |
int tmp = a[i]; | |
int j; | |
/* Shift elements down until insertion point found. */ | |
for (j = i - 1; j >= 0 && a[j] > tmp; j--) { | |
a[j + 1] = a[j]; | |
} | |
/* insert */ | |
a[j + 1] = tmp; | |
} | |
} | |
int main (void) { | |
char buf[BUFSIZ]; | |
while (fgets(buf, sizeof buf, stdin)) { | |
int n = (int)strlen(buf); | |
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) { | |
n--; | |
} | |
if (n > 0) { | |
insertSort(buf, n); | |
} | |
printf("%s", buf); | |
} | |
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
#/usr/bin/env python | |
# Example usage: | |
# cat /usr/share/dict/words | tr -cd '[:alpha:]\n' | tr '[:lower:]' '[:upper:]' | sortline.py | sort | uniq -c | awk '$1 > 1' | |
# Install: | |
# install -p sortline.py ~/bin | |
import sys | |
import fileinput | |
sys.stdout.writelines( | |
''.join(sorted(line.strip())) + '\n' | |
for line in fileinput.input() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment