title: What is wrong with this sorting algorithm in C?
body: <p>I was attempting a problem where I had to arrange a list of 5000 first names alphabetically (The names were stored in a text file <code>"names.txt"</code>). As seen from my code below, I created a 2D array <code>names[n][m]</code> to store the names. </p>
  
  <p>For every single name, I compare it against all the other names alphabetically. Whenever the i-th name is alphabetically larger than another, there will be an increment to its alphabetical ranking stored in its array element <code>rank[i]</code>. For example, when <code>"Mary"</code> is compared to <code>"Denise"</code>, Mary's rank will be incremented by 1 since it is alphabetically larger than Denise. All the ranks start from 1.</p>
  
  <p>This seemed to work as it was successful when tested with the example provided in the question. However, the final answer I obtained was incorrect. More importantly, I discovered that several of the names shared the same ranking (i.e. I checked and found that <code>"Columbus"</code> and <code>"Colt"</code> both have the same ranking). I'm not sure why or where my algorithm is flawed, as it seems logically sound(?) to me. I tried making my code more readable by adding a few comments, and I would appreciate if someone could explain my mistake to me. I've only been coding for about a few days, so forgive me if I had committed any rookie mistakes. Thanks for your time!  </p>
  
  <p>Link to problem: <a href="https://projecteuler.net/problem=22" rel="nofollow noreferrer">https://projecteuler.net/problem=22</a></p>
  
  <p>EDIT: The code is slightly truncated (I omitted the final step where I just added all the scores together). But the error I talked about is only relevant to the provided code. Thanks!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main() {
      FILE *fp;
      int i, j;
      int a = 0;
      int b = 0;
      fp = fopen("names.txt", "r");
      char names[5200][30] = { 0 };
      int rank[5200] = { 0 }; //Rank corresponds to their alphabetical positions
      unsigned long long score[5200] = { 0 };
      unsigned long long sum = 0;
      for (i = 0; i &lt; 5200; i++) {
          (rank[i])++;  //All the rankings start from 1.
      }
      for (i = 0; !feof(fp); i++) {
          fscanf(fp, "\"%[A-Z]\",", &amp;names[i]); //Scanning and storing the names from the file into the array.
      }
  
      for (i = 0; i &lt; 5200; i++) {
          for (j = 0; j &lt; 5200; j++) {
              if (i != j &amp;&amp; names[i][0] != 0 &amp;&amp; names[j][0] != 0) {
                  while (names[i][a] == names[j][a]) {  //If the ith word and jth word have the same letter, then increment a (which advances to the next letter).
                      a++;
                  }
                  if (names[i][a] &gt; names[j][a]) { 
                      (rank[i])++; //If the ith word has a larger letter than the jth word, there will be an increase in its rank.
                  } else
                  if (names[j][a] == 0 &amp;&amp; names[i][a] != 0) { 
                      (rank[i])++; //If the jth word is shorter than the ith word, then i also increases its rank.
                  }
              }
              a = 0;
          }
          for (a = 0; a &lt; 30; a++) {
              if (names[i][a] != 0 &amp;&amp; names[i][0] != 0) {
                  score[i] += (names[i][a] - 64); //Sum up the alphabetical value (as per the question) for each name.
              }
          }
          score[i] = (rank[i]) * (score[i]);
      }
  </code></pre>