views:

65

answers:

3

How would I modify a selectionSort function to search an array of strings?

void selectionSort (int array[], int size)
{
    int startScan, min Index, minValue;
    for (startScan = 0; startScan<(size-1); startScan++)
    {
        minIndex=startScan;
        minValue=array[startScan];
        for(int index = startScan + 1;index<size;index++)
        {
            if (array[index] < minValue)
            {
                minValue=array[index];
                minIndex=index;
            }
        }
    }
}
A: 

Create a functor that accepts an array of strings and handles the comparison however you want (if STL).

Michael Dorgan
+1  A: 

As I understand your question, you need to generalise ">" to strings -- you can obviously use some library function (for STL strings, > is defined), but if it is a homework, you are probably in need to write your own. If we are limited to ASCII, it is pretty straightforward while ASCII codes of letters hold alphabetical order ((int)'A'<(int)'B').
To compare strings, you should start with first letters of two strings, if they are not equal return the result of their comparison, and if they are the same proceed to the next pair.

mbq
A: 

The < and > operators are already able to handle comparing strings in alphabetical order. just overload the function to have a string array [] instead of a int array [] in the parameters. The one problem is that these operators are case sensitive so you will need to convert all the characters in the string to upper or lower case before doing the check.