C Program to implement Selection sort using functions

Levels of difficulty: / perform operation:

Write a C program to sort given N elements using SELECTION sort method using functions :
a) To find maximum of elements
b) To swap two elements
Selection sort is comparison based sorting technique, It finds the minimum value in list and swaps that value to the first position and so on. Selection sort is inefficient on larger data.


source code :#include<stdio.h>

void selection_sort(int a[],int num);
void display(int a[],int num);
int main()
{
int num,i,a[100];
printf("\nEnter the size of the array\n");
scanf("%d",&num);
printf("\n Enter the elements one by one\n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
selection_sort(a,num);
display(a,num);
return 0;
}

void selection_sort(int a[],int n)
{
int i,j,t,m;

for(i=0;i<n-1;i++)
{
m=i;
for(j=i+1;j<n;j++)
 {
 if(a[j]<a[m])
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
m=j;
 }

t=a[i];
a[i]=a[m];
a[m]=t;
}

}

void display(int a[],int num)
{
int i;
printf("\n the sorted elements are\n");
for(i=0;i<num;i++)
printf("%d\t",a[i]);
}

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation