Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What algorithm is this?

I cannot seem to make sense of this particular algorithm. It seems to be a bubblesort but not in a traditional sense. What is it?

public static void main(String[] args) 
{
    double[] a = {0.75, 0.5, 1.0};                          
    sort(a);

    for (int i = 0; i < a.length; i++)
        System.out.println(a[i]);
}

public static void sort(double[] tal)
{
    double p = 0;
    int k = 0;

    for (int i = 0; i < tal.length - 1; i++)
    {
        k = i;
        for (int j = i + 1; j < tal.length; j++)
        {
            if (tal[j] < tal[k])
                k = j;
        }

        p = tal[i];
        tal[i] = tal[k];
        tal[k] = p;
    }
}
like image 265
user3071133 Avatar asked Nov 27 '22 11:11

user3071133


1 Answers

At first, I incorrectly thought it an Insertion sort. It is a Selection sort (from the Wikipedia entry)

Selection sort

like image 145
Elliott Frisch Avatar answered Dec 15 '22 03:12

Elliott Frisch