Selection Sort in C.( C program to execute selection Sort algorithm)

Selection Sort Concept  : -

  
                    Selection Sort is a In-place sorting algorithm.

                  In   Selection Sort program the Array  is divided into two parts  In which first element of array is compared with all of the remaining elements of array.

                        So we clearly understand that it after each iteration we will gate smallest element of array at most left hand side or end of array.

The selection Sort is a very efficient then bubble sort , Insertion sort .
     
                      because the number of iterations as well the comparison in selection sorts less than the concept of bubble sorting and insertion sorting.

let's create C program for selection Sort.

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[30],i,j,pos,n,temp;
    clrscr();
    printf("Enter size of Array :- ");
    scanf("%d",&n);
    printf("Enter %d elements in Array:--->\n",n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0; i<n-1; i++)
    {
        pos=i;
        for(j=i+1; j<n; j++)
        {
            if(a[j]<a[pos])
            {
                temp=a[pos];
                a[pos]=a[j];
                a[j]=temp;
            }
        }
    }
    printf("Elements of Array After Selection Sort are:\n");
    for(i=0; i<n; i++)
    {
        printf("%d\n",a[i]);
    }
    return 0;
}

*** Input && Output ***

Post a Comment

0 Comments