Write a C Program to Implement Selection Sort using Recursion

By | 27.11.2016

Selection Sort using Recursion


Write a C Program to Implement Selection Sort using Recursion. Here’s simple Program to Implement Selection Sort using Recursion in C Programming Language.


Problem : :


This C Program implements a Selection sort. Selection sort works by finding the smallest unsorted item in the list and swapping it with the item in the current position. It is used for sorting unsorted list of elements.

Here is the source code of the C Program to Implement Selection Sort using Recursion. The C Program is successfully compiled and run on a Windows system. The program output is also shown below.


SOURCE CODE : :


/*  C Program to Implement Selection Sort using Recursion  */

#include <stdio.h>
 
void selection(int [], int, int, int, int);
 
int main()
{
    int list[30], size, temp, i, j;
 
    printf("Enter the size of the list: ");
    scanf("%d", &size);
    printf("Enter the elements in list:\n");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &list[i]);
    }
    selection(list, 0, 0, size, 1);
    printf("The sorted list in ascending order is\n");
    for (i = 0; i < size; i++)
    {
        printf("%d  ", list[i]);
    }
 
    return 0;
}
 
void selection(int list[], int i, int j, int size, int flag)
{
    int temp;
 
    if (i < size - 1)
    {
        if (flag)
        {
            j = i + 1;
        }
        if (j < size)
        {
            if (list[i] > list[j])
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
            selection(list, i, j + 1, size, 0);
        }
        selection(list, i + 1, 0, size, 1);
    }
}

OUTPUT : :


/*  C Program to Implement Selection Sort using Recursion  */

Enter the size of the list: 5
Enter the elements in list:
2
4
6
1
3
The sorted list in ascending order is
1  2  3  4  6

Above is the source code for C Program to Implement Selection Sort using Recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments