fork download
  1. #define SIZE 7
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void show(string ar[], int len, const char *caption = "") {
  7. cout << caption << " - ";
  8. for (int i=0; i<len; ++i) {
  9. cout << ar[i] << ", ";
  10. }
  11. cout << endl;
  12. }
  13.  
  14. void verydumbsort(string ar[], int len) {
  15. string arbuf[26][SIZE];
  16. int arb_i[26] = { 0 };
  17.  
  18. for (int j=0; j<len; ++j) {
  19. char initial = ar[j][0];
  20. if (initial >= 'a') {
  21. initial -= ('a' - 'A');
  22. }
  23. initial -= 65;
  24. // cout << ar[j] << '\t' << ((int) initial) << endl;
  25. arbuf[initial][arb_i[initial]] = ar[j];
  26. ++arb_i[initial];
  27. }
  28.  
  29. /*
  30. for (int k=0; k<26; ++k) {
  31. cout << "buffer " << ((char) (k+65));
  32. show(arbuf[k], SIZE);
  33. }
  34. */
  35.  
  36. int ar_index = 0;
  37. for (int m=0; m<26; ++m) {
  38. for (int n=0; n<arb_i[m]; ++n) {
  39. if (arbuf[m][n].length() > 0) {
  40. ar[ar_index] = arbuf[m][n];
  41. ++ar_index;
  42. }
  43. }
  44. }
  45. }
  46.  
  47. int main() {
  48. string word[SIZE];
  49. word[0]="Hello";
  50. word[1]="World";
  51. word[2]="Bye";
  52. word[3]="ggyy";
  53. word[4]="bison";
  54. word[5]="hotel";
  55. word[6]="benny";
  56.  
  57. show(word, SIZE, "before");
  58.  
  59. verydumbsort(word, SIZE);
  60.  
  61. show(word, SIZE, "after");
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
before - Hello, World, Bye, ggyy, bison, hotel, benny, 
after - Bye, bison, benny, ggyy, Hello, hotel, World,