In this example we explain selection sort in data structure. A selection sort is slightly more complicated. but relatively easy to understand and to code. It is one of the slow sorting technique. Given an unsorted array of integer values, a selection sort visits each element of the array, in turn. As it visits an element, it employs a second index to scan the array from the present location to the end while it identifies the least (smallest) value in that segment of the array. It then swaps that least value with the current value. Because one index scans the array once while another index scans part of the array each time, this sort algorithm is also O(n2).

Simple steps for selection sort. [Assume we are sorted the n items in array A]
for i = 1 to n do

for j = j+1 to n do

if A[i] > A[j] then swap(A[i],A[j])

Example: C program, Function to arrange number in ascending order using Selection sort technique.

void selection_sort(int a[],int n);
    int i,j,pos,small,temp;
        small=a[i]; /* Initial small number in ith pass */
        pos=i; /* Position of smaller number */
        /* Find the minimum of remaining elements along with the position */
        /* Exchange ith item with least item */
        a[pos] = a[i];

void main()
    int i,n,a[20];
    printf("Enter the number of elements to sort");
    printf("Enter %d elements to sort ",n);
    printf("The sort elements are");


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (, DZone MVB and founder of

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.