Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selection sort with strings

Okay, I've been using this code to do a selection sort on integers:

public void selectSort(int [] arr)
{
    //pos_min is short for position of min
    int pos_min,temp;

    for (int i=0; i < arr.Length-1; i++)
    {
        pos_min = i; //set pos_min to the current index of array

        for (int j=i+1; j < arr.Length; j++)
        {
            if (arr[j] < arr[pos_min])
            {
                //pos_min will keep track of the index that min is in, this is needed when a swap happens
                pos_min = j;
            }                                          
        }

        //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
        if (pos_min != i)
        {
            temp = arr[i];
            arr[i] = arr[pos_min];
            arr[pos_min] = temp;
        }
    }
}

but now I want to run the same algorithm on a string list instead.
How could that be accomplished? It feels really awkward and like you would need additional loops to compare multiple chars of different strings..?
I tried a lot, but I couldn't come up with anything useful. :/

Note: I know, selection sort isn't very efficient. This is for learning purposes only. I'm not looking for alternative algorithms or classes that are already part of C#. ;)

like image 797
Forivin Avatar asked Dec 19 '15 21:12

Forivin


2 Answers

IComparable is an interface that gives us a function called CompareTo, which is a comparison operator. This operator works for all types which implement the IComparable interface, which includes both integers and strings.

// Forall types A where A is a subtype of IComparable
public void selectSort<A>(A[] arr)
where A : IComparable
{
    //pos_min is short for position of min
    int pos_min;
    A temp;

    for (int i=0; i < arr.Length-1; i++)
    {
        pos_min = i; //set pos_min to the current index of array

        for (int j=i+1; j < arr.Length; j++)
        {
            // We now use 'CompareTo' instead of '<'
            if (arr[j].CompareTo(arr[pos_min]) < 0)
            {
                //pos_min will keep track of the index that min is in, this is needed when a swap happens
                pos_min = j;
            }                                          
        }

        //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
        if (pos_min != i)
        {
            temp = arr[i];
            arr[i] = arr[pos_min];
            arr[pos_min] = temp;
        }
    }
}
like image 166
erisco Avatar answered Nov 02 '22 17:11

erisco


The System.String class has a static int Compare(string, string) method that returns a negative number if the first string is smaller than the second, zero if they are equal, and a positive integer if the first is larger.

By "smaller" I mean that it comes before the other in the lexical order and by larger that it comes after the other in lexical order.

Therefore you can compare String.Compare(arr[j], arr[pos_min]) < 0 instead of just arr[j] < arr[pos_min] for integers.

like image 28
Olivier Jacot-Descombes Avatar answered Nov 02 '22 19:11

Olivier Jacot-Descombes