Wednesday 2 July 2014

Selection Sort

#include<conio.h>
#include<stdio.h>
main()
{
int n;
puts("Enter the size of the araay");
scanf("%d",&n);
int i,j,arr[n],min,pos,temp;
puts("Enter the values in the array");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<n;i++)
{
min=arr[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(min>arr[j])
{min=arr[j];
pos=j;
}
}
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}

puts("now the sorted array is:");
for(i=0;i<n;i++)
{
printf("\n %d",arr[i]);
}
getch();
return 0;
}

No comments:

Post a Comment