#define SIZE 7
#include <iostream>
using namespace std;
void show(string ar[], int len, const char *caption = "") {
cout << caption << " - ";
for (int i=0; i<len; ++i) {
cout << ar[i] << ", ";
}
cout << endl;
}
void verydumbsort(string ar[], int len) {
string arbuf[26][SIZE];
int arb_i[26] = { 0 };
for (int j=0; j<len; ++j) {
char initial = ar[j][0];
if (initial >= 'a') {
initial -= ('a' - 'A');
}
initial -= 65;
// cout << ar[j] << '\t' << ((int) initial) << endl;
arbuf[initial][arb_i[initial]] = ar[j];
++arb_i[initial];
}
/*
for (int k=0; k<26; ++k) {
cout << "buffer " << ((char) (k+65));
show(arbuf[k], SIZE);
}
*/
int ar_index = 0;
for (int m=0; m<26; ++m) {
for (int n=0; n<arb_i[m]; ++n) {
if (arbuf[m][n].length() > 0) {
ar[ar_index] = arbuf[m][n];
++ar_index;
}
}
}
}
int main() {
string word[SIZE];
word[0]="Hello";
word[1]="World";
word[2]="Bye";
word[3]="ggyy";
word[4]="bison";
word[5]="hotel";
word[6]="benny";
show(word, SIZE, "before");
verydumbsort(word, SIZE);
show(word, SIZE, "after");
return 0;
}
I2RlZmluZSBTSVpFIDcKI2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgc2hvdyhzdHJpbmcgYXJbXSwgaW50IGxlbiwgY29uc3QgY2hhciAqY2FwdGlvbiA9ICIiKSB7Cgljb3V0IDw8IGNhcHRpb24gPDwgIiAtICI7Cglmb3IgKGludCBpPTA7IGk8bGVuOyArK2kpIHsKCQljb3V0IDw8IGFyW2ldIDw8ICIsICI7Cgl9Cgljb3V0IDw8IGVuZGw7Cn0KCnZvaWQgdmVyeWR1bWJzb3J0KHN0cmluZyBhcltdLCBpbnQgbGVuKSB7CglzdHJpbmcgYXJidWZbMjZdW1NJWkVdOwoJaW50IGFyYl9pWzI2XSA9IHsgMCB9OwoJCglmb3IgKGludCBqPTA7IGo8bGVuOyArK2opIHsKCQljaGFyIGluaXRpYWwgPSBhcltqXVswXTsKCQlpZiAoaW5pdGlhbCA+PSAnYScpIHsKCQkJaW5pdGlhbCAtPSAoJ2EnIC0gJ0EnKTsKCQl9CgkJaW5pdGlhbCAtPSA2NTsKCQkvLyBjb3V0IDw8IGFyW2pdIDw8ICdcdCcgPDwgKChpbnQpIGluaXRpYWwpIDw8IGVuZGw7CgkJYXJidWZbaW5pdGlhbF1bYXJiX2lbaW5pdGlhbF1dID0gYXJbal07CgkJKythcmJfaVtpbml0aWFsXTsKCX0KCQoJLyoKCWZvciAoaW50IGs9MDsgazwyNjsgKytrKSB7CgkJY291dCA8PCAiYnVmZmVyICIgPDwgKChjaGFyKSAoays2NSkpOwoJCXNob3coYXJidWZba10sIFNJWkUpOwoJfQoJKi8KCQoJaW50IGFyX2luZGV4ID0gMDsKCWZvciAoaW50IG09MDsgbTwyNjsgKyttKSB7CgkJZm9yIChpbnQgbj0wOyBuPGFyYl9pW21dOyArK24pIHsKCQkJaWYgKGFyYnVmW21dW25dLmxlbmd0aCgpID4gMCkgewoJCQkJYXJbYXJfaW5kZXhdID0gYXJidWZbbV1bbl07CgkJCQkrK2FyX2luZGV4OwoJCQl9CgkJfQoJfQp9CgppbnQgbWFpbigpIHsKCXN0cmluZyB3b3JkW1NJWkVdOwoJd29yZFswXT0iSGVsbG8iOwoJd29yZFsxXT0iV29ybGQiOwoJd29yZFsyXT0iQnllIjsKCXdvcmRbM109ImdneXkiOwoJd29yZFs0XT0iYmlzb24iOwoJd29yZFs1XT0iaG90ZWwiOwoJd29yZFs2XT0iYmVubnkiOwoKCXNob3cod29yZCwgU0laRSwgImJlZm9yZSIpOwoJCgl2ZXJ5ZHVtYnNvcnQod29yZCwgU0laRSk7CgkKCXNob3cod29yZCwgU0laRSwgImFmdGVyIik7CgkKCXJldHVybiAwOwp9
before - Hello, World, Bye, ggyy, bison, hotel, benny,
after - Bye, bison, benny, ggyy, Hello, hotel, World,