0
Why my selection sort elements not print or sort array
When i print elements using for loop and use i<=n then its considered last elements zero and remove = the print garbage value. Solve it https://code.sololearn.com/c84lw8NRUhAq/?ref=app
1 Odpowiedź
0
#include<stdio.h>
int main()
{
int a[20],i,j,n,loc,temp,min;
printf("enter size of array");
scanf("%d", &n);
printf("enter element :");
for ( i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for ( i = 0; i < n; i++)
{
min = a[i];
loc = i;
for ( j = i + 1; j < n; j++)
{
if(a[j]<a[i])
{
min = a[j];
loc = j;
}
}
if (loc!=i)
{
temp = a[i];
a[i] = a[loc];
a[loc] = temp;
}
}
printf(" \nmin value is %d\n", a[0]);
printf("sorted element is :");
for ( i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
return 0;
}