Sunday 27 September 2015

Selection sort

Now today i will be discussing about selection sort too . in this sorting we first try to find the maximum element and then we place it in array's first position . 
After this step we find second greatest element and place it in second position of array and so on. 
So the simple program for selection sort is

 #include<stdio.h>
void main()
{
    int array1[]={32,67,8,54,21,43,76,83,12,32},i;
    selection(array1,9);
    for(i=0;i<=9;i++)
    {
    printf("%d\n",array1[i]);
    }
}
void selection(int *A,int n )
{

    int i,j,temp;
    for(i=0;i<=n-1;i++)
    {
        for (j=i;j<=n;j++)
        {
            if (A[i]>A[j])
                                     /*checking if the element that we are comparing is smaller than our assumption of minimum change it */
            {
                temp = A[j];
                A[j]=A[i];
                A[i]=temp;
            }
        }
    }
}




No comments:

Post a Comment