title: Hash table sorting and execution time
body: <p>I write a program to count the frequency word count using hash table, but I don't how to sort it.</p>
  
  <p>I use struct to store value and count.</p>
  
  <p>My hash code generate function is using module and my hash table is using by linked list.</p>
  
  <p>1.My question is how do I sort them by frequency?</p>
  
  <p>2.I am wondering that why my printed execute time is always zero, but I check it for many time. Where is the wrong way?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;ctype.h&gt;
  
  #define  HASHSIZE 29989 
  #define  FACTOR 31
  #define  VOCABULARYSIZE 30
  typedef struct HashNode HashNode;
  struct HashNode{
      char* voc;//vocabulary
      int freq;//frequency
      struct HashNode *next;//pointed to the same hashcode 
                            //but actually are different numbers
  };
  
  HashNode *HashTable[HASHSIZE] = {NULL,0,NULL};//an array of pointers
  
  unsigned int HashCode(const char *pVoc){//generate hashcode
      unsigned int index = 0;
      int n = strlen(pVoc);
      int i = 0;
      for(; i &lt; n; i ++)
          index = FACTOR*index + pVoc[i];
      return index % HASHSIZE;
  }
  
  void InsertVocabulary(const char *pVoc){//insert vocabulary to hash table
      HashNode *ptr;
      unsigned int index = HashCode(pVoc);
      for(ptr = HashTable[index]; ptr != NULL; ptr = ptr -&gt; next){//search if already exist
          if(!strcmp (pVoc, ptr -&gt; voc)){
              (ptr-&gt;freq)++;
              return;          
          }        
      }
      ptr = (HashNode*)malloc(sizeof(HashNode));//if doesn't exist, create it
      ptr -&gt; freq = 1; 
      ptr -&gt; voc = (char*)malloc(strlen(pVoc)+1);
      strcpy(ptr -&gt; voc, pVoc);
      ptr -&gt; next = HashTable[index];
      HashTable[index] = ptr;
  }
  
  void ReadVocabularyTOHashTable(const char *path){
      FILE *pFile;
      char buffer[VOCABULARYSIZE];
      pFile = fopen(path, "r");//open file for read
      if(pFile == NULL)
          perror("Fail to Read!\n");//error message
      char ch;
      int i =0;
      do{
          ch = fgetc(pFile);
          if(isalpha(ch))
              buffer[i++] = tolower(ch);//all convert to lowercase                
          else{
              buffer[i] = '\0';//c-style string
              i = 0;
              if(!isalpha(buffer[0]))
                  continue;//blank line
              else //printf("%s\n",buffer);
                  InsertVocabulary(buffer);
          }
      }while(ch != EOF);
      fclose(pFile);
  }
  
  void WriteVocabularyTOHashTable(const char *path){
      FILE *pFile;
      pFile = fopen(path, "w");
      if(pFile == NULL)
          perror("Fail to Write\n");
      int i = 0;
      for(; i &lt; HASHSIZE; i++){
          HashNode *ptr = HashTable[i];
          for(; ptr != NULL; ptr = ptr -&gt; next){
              fprintf(pFile, "Vocabulary:%s,Count:%d\n", ptr -&gt; voc, ptr -&gt; freq);
              if(ptr -&gt; next == NULL)
                  fprintf(pFile,"\n");
          }
      }
      fclose(pFile);
  }
  
  int main(void){
      time_t start, end;
      time(&amp;start);
      ReadVocabularyTOHashTable("test.txt");
      WriteVocabularyTOHashTable("result.txt");
      time(&amp;end);
      double diff = difftime(end,start);
      printf("%.21f seconds.\n", diff); 
      system("pause");
      return 0;     
  }
  </code></pre>