My Project - Video Waker Alarm
Get Video alarm on Google Play

C program for Selection Sort


C program for Selection Sort. Selection Sort is a simple-unstable-in-place Sorting Algorithm. Selection sort is efficient when memory is limited and over a small set of data, while make it more complex and inefficient on large set of data and become more worse as compare to insertion sortYou can also see other sorting algorithm implementation like, Heap sortbubble sort etc.

Selection Sort Algorithm:

Algorithm scans through the data, looking for either the smallest or the largest element(depending upon you) in the set and then swaps it with the first or the last element of Data set. Now find the smallest item from the data set excluding the first that is already sorted, and replace that element with the Second element. Similarly the loop continues until the last two items in the data set. Now the Data is sorted.

Note: The above Selection sort, described uses swapping of data which makes the algorithm Unstable, while Selection sort can also be implemented as Stable sort, by selecting the smallest element and putting the element before the first element of the list i.e. instead of swapping the first element with the smallest element, we are sweeping the first element to second position, second to third and so on.

C program for Selection Sort – Unstable In-place:

#include <stdio.h>

//Selection Sort function to Sort Integer array list
int *selectionSort(int array[],int n)
{
    int j,temp,i;

    //Iterate start from first element
    for (i = 0; i < n; i++)
    {
        //Iterate and compare till it satisfies condition
        for(j = i+1; j < n; j++)
        {
            if(array[i] > array[j])
            {//Swaping operation
                temp = array[i];
                array[i]   = array[j];
                array[j] = temp;
            }
        }
    }
    //return Sorted array
    return array;
}

int main()
{
    //declaring variables
    int array[1000],n,i;

    //Number of elements in array form user input
    printf("Enter the number of element you want to Sort : ");
    scanf("%d",&n);

    //code to ask to enter elements from user equal to n
    printf("Enter Elements in the list : ");
    for(i = 0; i < n; i++)
    {
        scanf("%d",&array[i]);
    }

    //calling selectionSort function defined above and gettting
    //sorted array in sortArray variable
    int *sortArray = selectionSort(array,n);

    //print sorted array
          printf("Sorted list : ");
    for(i = 0; i < n; i++ )
    {
        printf("%d\t",sortArray[i]);
    }
}

Output of C program for Selection Sort

Output of C program for selection sort
Output of C program for selection sort

Example of Selection Sort:

Still getting problem or confused how Selection Sort algorithm works, then see the images below, demonstrating the operation performed to sort the array:

C program for Selection Sort
Selecting the first smallest and swapping
C program for Selection Sort
Selecting Second smallest from the list and comparing with the second position
C program for Selection Sort
Selecting the third element and comparing with the remaining element and swapping
C program for Selection Sort
Already sorted
C program for Selection Sort
Sorted : C program for Selection Sort

Hope this example is helpful. Do comment below with your experience, problem or code. Your comment is very valuable to us.


One thought on “C program for Selection Sort”

Leave a Reply

Your email address will not be published. Required fields are marked *