Q:

Write a C Program to Implement Selection Sort using Functions

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Problem : :


This C Program implements selection sort method using functions. Selection sort is among the simplest of sorting techniques.

It works as follows:First find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.

Here is the source code of the C Program to Implement Selection Sort using Functions. 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 Functions  */

#include <stdio.h>

int findmax(int b[10], int k);
void exchang(int b[10], int k);

int main()
{
    int array[10];
    int i, j, n, temp;

    printf("How many elements u want to Sort :: ");
    scanf("%d", &n);

    printf("\nEnter [ %d ] elements below to be Sorted :: \n",n);

    for (i = 0; i < n; i++)
    {
        printf("\nEnter [ %d ] Element :: ",i+1);
        scanf("%d", &array[i]);
    }

    printf("\nUnsorted Elements in List are :: \n\n");
    for (i = 0; i < n ; i++)
    {
        printf("%d  ", array[i]);
    }

    /*  Selection sorting begins */

    exchang(array, n);

    printf("\n\nAfter implementing Selection Sort, Sorted List is :: \n\n");
    for (i = 0; i < n; i++)
    {
        printf("%d  ", array[i]);
    }

    printf("\n");

    return 0;
}

/*  function to find the maximum value */
int findmax(int b[10], int k)
{
    int max = 0, j;
    for (j = 1; j <= k; j++)
    {
        if (b[j] > b[max])
        {
            max = j;
        }
    }
    return(max);
}
void exchang(int b[10], int k)
{
    int  temp, big, j;
    for (j = k - 1; j >= 1; j--)
    {
        big = findmax(b, j);
        temp = b[big];
        b[big] = b[j];
        b[j] = temp;
    }
    return;
}

OUTPUT : :


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

How many elements u want to Sort :: 7

Enter [ 7 ] elements below to be Sorted ::

Enter [ 1 ] Element :: 3

Enter [ 2 ] Element :: 1

Enter [ 3 ] Element :: 5

Enter [ 4 ] Element :: 7

Enter [ 5 ] Element :: 0

Enter [ 6 ] Element :: 4

Enter [ 7 ] Element :: 2

Unsorted Elements in List are ::

3  1  5  7  0  4  2

After implementing Selection Sort, Sorted List is ::

0  1  2  3  4  5  7

Process returned 0

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to implement Merge Sort using Recursion... >>
<< C Program to implement Merge Sort using Recursion...