Breaking Posts

6/trending/recent

Hot Widget

Type Here to Get Search Results !

C Program to Sort an Array using SELECTION SORT




#include <stdio.h>
void selection_sort();
int a[30], n;
void main()
{
    int i;
    printf("\nEnter size of an array: ");
    scanf("%d", &n);
    printf("\nEnter elements of an array:\n");
    for(i=0; i<n; i++)
        scanf("%d", &a[i]);
    selection_sort();
    printf("\n\nAfter sorting:\n");
    for(i=0; i<n; i++)
        printf("\n%d", a[i]);
    getch();
}
void selection_sort()
{
    int i, j, min, temp;
    for (i=0; i<n; i++)
    {
        min = i;
        for (j=i+1; j<n; j++)
        {
            if (a[j] < a[min])
                min = j;
        }
        temp = a[i];
        a[i] = a[min];
        a[min] = temp;
    }
}


Expected Output:

Enter size of an array: 8

Enter elements of an array:
68 45 78 14 25 65 55 44


After sorting:

14
25
44
45
55
65
68
78

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.