DATA STRUCTURES



Admission Enquiry Form

Selection sort in an Array.

In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array.The array is divided into two parts, first is sorted part, and another one is the unsorted part. Initially, the sorted part of the array is empty, and unsorted part is the given array. Sorted part is placed at the left, while the unsorted part is placed at the right.


Example




Program of Selection Sort in an Array.

#include<stdio.h>

void main()
{
    int arr[50],size,i,j,min,swap;

    printf("Enter size of array: \n");
    scanf("%d",&size);

    printf("Enter Elements of arr: \n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<size-1;i++)
    {
        min=i;
        for(j=i+1;j<size;j++)
        {
            if(arr[j]<arr[min])
            {
                min=j;
            }
        }
        if (min != i)
        {
           swap = arr[i];
           arr[i] = arr[min];
           arr[min] = swap;
        }
    }
    printf("Elements Of array After sorting are:\n");
    for(i=0;i<size;i++)
    {
        printf("\n%d",arr[i]);
    }
}//end of main


Output

   Enter size of array:
   3
   Enter Elements of arr:
   1
   6
   2
   Elements Of array After sorting are:
   1
   2
   6