sort function issue - backwards?

I don't know if posting my entire code is necessary but will do so in the event that it may be needed.

This sortWords function is sorting backwards and it is running infinitely. I am having trouble figuring out why - any pointers as to where to look and what may be the issue would be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 #include <iostream>
  #include <fstream>
  #include <string>
  #include <cstdlib>

  using namespace std;


  struct Stuff{
      string words;
      int g;
  };


  //declare functions

  void sortWords(Stuff Results[], int&);


  int main()  { 

  //sort words in array
  sortWords(Results, size);


  inf.close();
  return 0;
  }

//*************************sortWords*********************************************
//  Name:         sortWords
//  Description:  sort words and eliminate duplicates
//  Parameters:   Results, int size
//  Return:       none
//*******************************************************************************
	void sortWords(Stuff Results[], int& size)  {
		// fill in with the type of elements
 	
		for (int nStartIndex = 0; nStartIndex < size - 1; nStartIndex++) {

			// nSmallestIndex is the index of the smallest element
    		// we've encountered so far.
			int nSmallestIndex = nStartIndex;
       
			//Search thru evry element starting at nStartIndex + 1			
			for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < size; nCurrentIndex++) {

				// If the current element is smaller than our previously found smallest
             	if (Results[nCurrentIndex].word < Results[nSmallestIndex].word)
						//Store the index in nSmallestIndex
               			nSmallestIndex = nCurrentIndex;
						
        		 if (nSmallestIndex != nCurrentIndex) 
             			swap(Results[nSmallestIndex],    Results[nStartIndex]);
              			
               	

				cout << Results[nStartIndex].word << endl;  // for testing


			}// 2nd for loop
        
         
        }//1st for loop delimiter
         
  }//function delimiter
Last edited on
Well, I tried out your program with an input file that had 13 words in it and your program attempted to sort over 1000 strings in a very naive fashion.

I would suggest changing line 45 to:

sortWords(recResults, countW);
so that you're only sorting words you've extracted from the file and changing the sort function to something more like the following to minimize the number of elements you're swapping.

1
2
3
4
5
6
7
8
9
10
11
12
    for (unsigned i=0 ;  i < size-1 ;  ++i)
    {
        // find smallest index of the elements left which are unsorted:
        unsigned smallest = i ;
        for (int j=i+1;  j < size ;  ++j )
            if ( recResults[smallest].word > recResults[j].word )
                smallest = j ;
  
        // Make the current element the smallest of those we found.
        if ( smallest != i )
            swap( recResults[smallest], recResults[i] ) ; 
    }


Your code for counting letters in countL is guaranteed to be off by one. You should loop on something other than the eof condition.

I got it to work correctly - I had messed up a part in the sort function loop.

Changed to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void sortWords(Results recResults[], int& size)  {

		// fill in with the type of elements
		for (int nStartIndex = 0; nStartIndex < size - 1; nStartIndex++) {

			// nSmallestIndex is the index of the smallest element thus far.
			int nSmallestIndex = nStartIndex;
       
			//Search thru evry element starting at nStartIndex + 1			
			for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < size; nCurrentIndex++) {

				// If the current element is smaller than our previously found smallest
				if (recResults[nCurrentIndex].word < recResults[nSmallestIndex].word)

				//Store the index in nSmallestIndex
					nSmallestIndex = nCurrentIndex;

			}// 2nd for loop
        
			if (nSmallestIndex != nStartIndex) //<-------- changed this
				swap(recResults[nSmallestIndex], recResults[nStartIndex]);
          
		cout << "Start Print" << endl;
		cout << recResults[nStartIndex].word << endl;  // for testing     			 
		}//1st for loop delimiter
        
	}//function delimiter
Last edited on
Oh and I fixed the counter for lines too! Thank you for the assistance.
Topic archived. No new replies allowed.