1

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;
            }
        }
    }
}
6
  • Can you clarify the question a bit more? I can't think of a search algorithm that would be built upon selection sort. Commented Jul 15, 2010 at 20:53
  • Formatted the code to be more readable. However, it doesn't have an array of strings, or a single string, in it, so it still is way unclear what you mean by it. Commented Jul 15, 2010 at 21:11
  • That is the function used to sort. Right now it is configured for integers, how would I change it to use with character strings? Commented Jul 15, 2010 at 21:15
  • That's not a sort function; that finds the minimum value in the int array and its index. Commented Jul 15, 2010 at 21:29
  • @David -- it is a part of sorting; selsort iteratively finds the minimum in an array, removes it and puts on the end of the result array. Commented Jul 15, 2010 at 22:07

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.