# Problem with sorting algorithm

• 02-22-2006, 10:37 AM
MM4o
Problem with sorting algorithm
I had this assingment from my profesor in COS:
The program has to scan an input text, select every word (separated with white space or a punctuation characters); tofind all occurrences of a particular word(address of the word is the number of its first character); to create the index( word with attached list of addresses); and to sort words (with index alphabetically.
So far i have managed to solve every problem exept the sorting of words. Can someone help me fixing the bugs in the sorting algorithm.
Here is the code:
PHP Code:

``` #include <string> #include <iostream> using namespace std; #define MAX_STRING_SPACE        1000 #define MAX_NUM_STRINGS         250 #define MAX_STRING_SIZE         300 int bmsearch(char *text,char *pat,int *no,int *pos) {     int i,table[256];     int len=strlen(pat);     *no=0;     int compcount=0;     for(i=0;i<256;i++)table[i]=len;     for(i=0;i<len;i++)table[pat[i]]=len-(i+1);     int ptct=len-1;     while(ptct<strlen(text))     {         int count=0;         while(count<len)         {             if(text[ptct-count]!=pat[len-1-count]){compcount++;break;}             else count++;         }         if(count==len)         {             (*no)++;             *(pos+(*no)-1)=(ptct-count+1);             ptct+=len;         }         else         {ptct+=(table[text[ptct-count]]-count);}     }     return compcount; } main() {     char text[300],pat[100];     int no,count,pos[20],i;     cout<<"\nEnter the text string:";     gets(text);     cout<<"Enter the searched pattern:";     gets(pat);     count=bmsearch(text,pat,&no,pos);     cout<<"\n\nPattern string occoured "<<no<<" times.";     cout<<"\n"<<count<<" comparisons required";     cout<<"\nPositions of occourence:\n";     for(i=0;i<no;i++)         cout<<pos[i]<<"\n";         cout<<"\nThe index of the words"<<endl;     for (int j=0;j<300;j++)     {         if (j==0) cout<<j<<" - ";         if(text[j]==' '||text[j]=='.'||text[j]==','||text[j]=='!'&&(j+2)<strlen(text))             if (text[j+1]==' '||text[j+1]=='.'||text[j+1]==','||text[j]=='!'&&(j+2)<strlen(text))             {cout<<"\n"<<j+3<<" - ";j++;}             else                  cout<<"\n"<<j+2<<" - ";         else             if (j<strlen(text))                 cout<<text[j];     }     //Till here everything is ok. But i cannot make this sorting algorithm work         char string_space[MAX_STRING_SPACE];         char *strings[MAX_NUM_STRINGS];         /* Read the strings. */         char *next_space = string_space;         int inloc = 0;         while(text) {                 /* Find the length of the string and see if it fits. */             int length = strlen(text) + 1;             if(next_space + length >= string_space + MAX_STRING_SPACE)                 break;             if(inloc >= MAX_NUM_STRINGS)                 break;             /* Place the string into the structure. */             strings[inloc++] = next_space;             strcpy(next_space, text);             next_space += length;         }         cout<<"\n--------------------------------------------------\n";         /* Perform the sort.  Outer loop goes through destination of the minimum string. */         int strloc;         for(strloc = 0; strloc < inloc - 1; ++strloc)          {             /* Scan the remaining strings for ones smaller. */             int scan;             for(scan = strloc + 1; scan < inloc; ++scan)              {                 if(strcmp(strings[strloc], strings[scan]) > 0)                  {                     /* Exchange the strings. */                     char *tmp = strings[strloc];                     strings[strloc] = strings[scan];                     strings[scan] = tmp;                 }             }         }         /* Print 'em. */         for(strloc = 0; strloc < inloc; ++strloc)          {             cout<<strings[strloc]<<endl;         }                  return 0; }  ```
• 02-22-2006, 04:00 PM
jonnin
I think you need to use strcpy not assignment operations. Also, tmp is assigned the address of existing stuff not a true place holder -- that *looks* like a bug but I did not check it deeply.