Selection Sort

Properties

  • Not stable
  • O(1) extra space
  • Θ(n2) comparisons
  • Θ(n) swaps
  • Not adaptive

Discussion

From the comparions presented here, one might conclude that selection sort should never be used. It does not adapt to the data in any way (notice that the four animations above run in lock step), so its runtime is always quadratic.
However, selection sort has the property of minimizing the number of swaps. In applications where the cost of swapping items is high, selection sort very well may be the algorithm of choice.

Program C Code

//Selection//
void selection(int * gelendizi,int dizi_boyut)
{
    int i,j,temp;

    for (i=0; i<dizi_boyut-1; i++)
    {
        for (j=i+1; j<dizi_boyut; j++)
        {
            if (gelendizi[i]>gelendizi[j])
            {
                temp=gelendizi[j];
                gelendizi[j]=gelendizi[i];
                gelendizi[i]= temp;
            }
        }
    }
//   return gelendizi;
}


Hiç yorum yok:

Yorum Gönder