codescracker
c

C Program for Selection Sort



« Previous Program Next Program »

In this tutorial, we will learn about how to create a program in C that sorts an array using Selection sort technique. At last we have also created a function that can be used to sort any given array in ascending order using selection sort technique.

But before, going through the program, if you are not aware about how selection sort works, then refer to the step by step working of Selection Sort. Now let's move on and implement it in a C program.

C Selection Sort Program

To sort an array in ascending order using selection sort technique in C programming, you have to ask from user to enter the array size and its elements. Then apply the selection sort mechanism and print the sorted array as output just like the program given below:

// Selection Sort Program in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int size, arr[50], i, j, temp, small, count=0, index;
    printf("Enter size for Array: ");
    scanf("%d", &size);
    printf("Enter %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    for(i=0; i<(size-1); i++)
    {
        small = arr[i];
        for(j=(i+1); j<size; j++)
        {
            if(small>arr[j])
            {
                small = arr[j];
                count++;
                index = j;
            }
        }
        if(count!=0)
        {
            temp = arr[i];
            arr[i] = small;
            arr[index] = temp;
        }
        count=0;
    }
    printf("\nNow the Array after sorting is:\n");
    for(i=0; i<size; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore here is the sample run after successful build and run. This is the first snapshot of sample run:

selection sort in c programming

Now supply size for array say 5 and its element as 54 21 8 18 3 and press ENTER key to see the sorted array as shown in the second snapshot of sample run given here:

selection sort in c

Here is another sample run. This is the final snapshot:

c selection sort

C Selection Sort Program Explained

If you want to print array elements as step by step sorting of array elements shown on output screen along with its final array. Then you can modify the above code as shown below to print array elements after each sorting step:

// Selection Sort Program with all Steps in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int size, arr[50], i, j, temp, small, count=0, index;
    printf("Enter size for Array: ");
    scanf("%d", &size);
    printf("Enter %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    for(i=0; i<(size-1); i++)
    {
        small = arr[i];
        for(j=(i+1); j<size; j++)
        {
            if(small>arr[j])
            {
                small = arr[j];
                count++;
                index = j;
            }
        }
        if(count!=0)
        {
            temp = arr[i];
            arr[i] = small;
            arr[index] = temp;
        }
        printf("\nStep %d: ", i+1);
        for(j=0; j<size; j++)
            printf("%d ", arr[j]);
        printf("\n");
        count=0;
    }
    printf("\nNow the Array after sorting is:\n");
    for(i=0; i<size; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c sort array using selection sort

using Function

Let's create the same program, that is to sort an array in ascending order using selection sort technique, but this time, with the help of function. Here selection sort is implemeted inside a function named selsort() that uses two arguments, one is array and second is its size as shown in the program given below:

// Selection Sort Program using Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
void selsort(int arr[], int size);
int main()
{
    int size, arr[50], i;
    printf("Enter size for Array: ");
    scanf("%d", &size);
    printf("Enter %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    selsort(arr, size);
    printf("\nThe sorted Array is:\n");
    for(i=0; i<size; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}
void selsort(int arr[], int size)
{
    int i, j, temp, small, count=0, index;
    for(i=0; i<(size-1); i++)
    {
        small = arr[i];
        for(j=(i+1); j<size; j++)
        {
            if(small>arr[j])
            {
                small = arr[j];
                count++;
                index = j;
            }
        }
        if(count!=0)
        {
            temp = arr[i];
            arr[i] = small;
            arr[index] = temp;
        }
        count=0;
    }
}

Here is the final snapshot of the above program:

c selection sort using function

The concept used in above program is similar as used in first program except we have implemented a function selsort() here. All the selection sort technique is implemented inside this function. We only have to pass any array and its size to this function as its argument. And all the elements inside the array gets sorted in ascending order.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker