Sorting is a important technique to arrange a list. There are many sorting technique out there.Today on 6Tags we'll discuss Selection Sort In C using function. This is also a part of syllabus of B.Tech (CS).
In selection sort user input a list of values,the program will check first value of the list ,if it is greater than second element ,it will swap the value.Here is the code of Selectionsort.c
#include<stdio.h>
#include<conio.h>
void selection(int [],int); //declaring the function
void main(){
int n,i,a[100];
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the %d elements\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("the unsorted list\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
printf("the sorted list\n");
selection(a,n); //passing the value
getch();
}
void selection(int a[],int n) //calling the function
{int i,j,temp;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Output:
No comments:
Post a Comment