Write a program to Selection Sort using function in C program.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[50];
void select_sort(int[],int);
clrscr();
printf("How many Enter Element:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nInput Number:",i+1);
scanf("%d",&a[i]);
}
select_sort(a,n);
printf("Selection Sort List:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void select_sort(int a[],int n)
{
int j,k,i,min,temp;
for(i=0;i<n-1;i++)
{
min=i;
for(k=i+1;k<n;k++)
if(a[min]>a[k])
{
min=k;
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
}
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[50];
void select_sort(int[],int);
clrscr();
printf("How many Enter Element:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nInput Number:",i+1);
scanf("%d",&a[i]);
}
select_sort(a,n);
printf("Selection Sort List:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void select_sort(int a[],int n)
{
int j,k,i,min,temp;
for(i=0;i<n-1;i++)
{
min=i;
for(k=i+1;k<n;k++)
if(a[min]>a[k])
{
min=k;
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
}
Comments
Post a Comment