Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // dep stands for significant depth of words,
- // e.g. if def == 3, then "iiia" and "iiib" are treated as equal.
- // Occurence of a short word doesn't raise a bug, if enough trailing null-terminators
- void ssort(unsigned char **str, int num, int dep)
- {
- unsigned char **str_ = malloc(num * sizeof(char *));
- while (dep--) {
- int cnt[0x100] = {};
- for (int i = 0; i < num; ++i) {
- ++cnt[str[i][dep]];
- }
- for (int c = 1; c < 0x100; ++c) {
- cnt[c] += cnt[c-1];
- }
- for (int i = num-1; i >= 0; --i) {
- str_[--cnt[str[i][dep]]] = str[i];
- }
- memcpy(str, str_, num * sizeof(char *));
- }
- }
- int main(int argc, char **argv)
- {
- ssort(argv+2, argc-2, atoi(argv[1]));
- for (int i = 2; i < argc; ++i) puts(argv[i]);
- }
- /*
- $ ./testssort 1 suka blyad ne dala ebat
- blyad
- dala
- ebat
- ne
- suka
- $ ./testssort 1 kbl kal
- kbl
- kal
- $ ./testssort 2 kbl kal
- kal
- kbl
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement