title: quicker way to run this simple c++ program (sort array of structs)
body: <p>This program is going very slowly when I call the sort arrays function.  If i print out elements of the array before it is sorted it runs in seconds.  If not, it will take sometimes a minute to run with a  farily short text file.  My first command line argument this program takes is a "book" (a text file with all lowercase words) and the second command line argument(x) tells how many times the most frequent x words occured.  Any help on how to quicker sort the array of structs would be super helpful!</p>
  
  <pre><code>using namespace std;
  
  //void insertionSort(struct count array[],int size);
  void sortArray(struct count array[], int size);
  bool ExcludeCommon(string word);
  
  
  struct count
  {
  string word;
  int frequency;
  };
  
  int main(int argc, char *argv[])
  {
  ifstream filename;
  filename.open(argv[1]);
  ifstream num_words;
  
  int num = stoi((argv[2]));
  
  string one_word;
  int size = 100;
  count *count_array = new count[size];
  int total_non_comm=0;
  int words_in_arr = 0;
  int double_count=0;
  while (filename &gt;&gt; one_word)
  {   
      int i;
      int freq = 1;
      bool found = false;
  
      if(ExcludeCommon(one_word)==0)
      {
          if(total_non_comm&gt;=size)
              {
                  count *new2= new count[size*2];
                  for(int i=0; i&lt;size; i++)
                      {
                          new2[i]=count_array[i];
                          }
                  size = size*2;
                  delete[] count_array;
                  count_array=new2;
                  double_count++;
  
              }
  
          for(i=0; i&lt;=words_in_arr; i++)
          {
  
                  if(count_array[i].word == one_word)
                  {
                  count_array[i].frequency++;
                  found=true;
                  total_non_comm++;
                  }
  
          }
          if(found == false)
          {
              struct count add;
              add.word = one_word;
              add.frequency = freq++;
              count_array[words_in_arr]=add;
              words_in_arr++;
              total_non_comm++;
          }
  
          found = false;
  
      }
  
  }
  
   sortArray(count_array, size);
  
  for(int i=0; i&lt;num;i++)
  {
  cout&lt;&lt;count_array[i].frequency&lt;&lt; " - " &lt;&lt; count_array[i].word&lt;&lt;endl;
  }
  cout&lt;&lt;"#"&lt;&lt;endl;
  cout&lt;&lt;"Array doubled: "&lt;&lt;double_count-3&lt;&lt;endl;
  cout&lt;&lt;"#"&lt;&lt;endl;
  cout&lt;&lt;"Unique non-common words: " &lt;&lt;words_in_arr&lt;&lt;endl;
  cout&lt;&lt;"#"&lt;&lt;endl;
  cout&lt;&lt;"Total non-common words: " &lt;&lt; total_non_comm &lt;&lt;endl;
  
  
  
  };
  
  
  
  
  
  
  
  
  bool ExcludeCommon(string word)
  {
      if (word == "the" || word == "be" || word == "to" || word == "of" || 
  word == "and" || word == "a" || word == "in" || word == "that" || 
  word == "have" ||word == "i" || word == "it" || word == "for" || word == "not" || 
  word == "on" || word == "with" || word == "he" || word == "as" || 
  word == "you" || word == "do" || word == "at" || word == "this" || 
  word == "but" || word == "his" || word == "by" || word == "from"||
  word == "they" || word == "we" || word == "say" || word == "her" || 
  word == "she" || word == "or" || word == "an" || word == "will" || 
  word == "my" || word == "one" || word == "all" || word == "would" || 
  word == "there" || word == "their" || word == "what" || word == "so" || 
  word == "up" || word == "out" || word == "if" || word == "about" || 
  word == "who" || word == "get" || word == "which" ||word == "go" || word == "me")
  
  {return 1;
  }
  return 0;
  };
  
  
  void sortArray(struct count array[],int size)
  {
  int cur_pos = 0;
  string the_word;
  
  
     for(int i=0; i&lt;(size); i++)
  {
      for(int j=size-1; j&gt;=i; j--)
      {
          if((array[j+1].frequency)&gt;(array[j].frequency))
          {
              cur_pos = array[j].frequency;
              the_word = array[j].word;
              array[j].frequency = array[j+1].frequency;
              array[j].word = array[j+1].word;
              array[j+1].frequency = cur_pos;
              array[j+1].word = the_word;
  
  
          }
      }
  }
  
  };
  </code></pre>