Write a program to implement selection sort using function.


Input


#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 you 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


How many elements you want to Sort :: 5

Enter [ 5 ] elements below to be Sorted ::

Enter [ 1 ] Element :: 4

Enter [ 2 ] Element :: 2

Enter [ 3 ] Element :: 6

Enter [ 4 ] Element :: 0

Enter [ 5 ] Element :: 8

Unsorted Elements in List are ::

4  2  6  0  8 

After implementing Selection Sort, Sorted List is ::

0  2  4  6  8

No comments:

Post a Comment