it's urgent.
Can anybody tell me the program of selection sorting in c language.?
I have a program with me. I hope it would help you.
Sorting of numbers:
#include %26lt;stdio.h%26gt;
#include %26lt;conio.h%26gt;
void main()
{
int n, i, j, a[20];
clrscr();
scanf("%d",%26amp;n);
for (i=1;i%26lt;=n;i++)
scanf(%d",%26amp;a[i]);
for (i=1;i%26lt;n;i++)
{
for (j=i+1;j%26lt;=n;j++)
{
if (a[i] %26gt; a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
for (i=1;i%26lt;=n;i++)
printf("\n%d",a[i]);
getch();
}
Reply:/* Selection sort. */
#include %26lt;stdio.h%26gt;
#include %26lt;conio.h%26gt;
void main() {
int arr[5] = {25, 17, 31, 13, 2};
int i, j, temp;
clrscr();
printf("Selection sort.\n");
printf("\nArray before sorting:\n");
for (i = 0; i %26lt;= 4; i++)
printf("%d\t", arr[i]);
for (i = 0; i %26lt;= 3; i++) {
for (j = i + 1; j %26lt;= 4; j++) {
if (arr[i] %26gt; arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\n\nArray after sorting:\n");
for (i = 0; i %26lt;= 4; i++)
printf("%d\t", arr[i]);
getch();
}
Reply:try this i think it could help you
#include%26lt;stdio.h%26gt;
#include%26lt;stdlib.h%26gt;
#define size 10
void selection(int a[],int array_size);
int main(){
int a[size];
int i;
//srand(getpid());
for(i=0;i%26lt;size;i++)
a[i]=rand();
selection(a,size);
printf("numbers sorted:\n");
for(i=0;i%26lt;size;i++)
printf("a[%d]=%d\n",i,a[i]);
return 0;
}
void selection(int a[],int array_size)
{
int i,j,min,temp;
for(i=0;i%26lt;array_size-1;i++)
{
min=i;
for(j=i+1;j%26lt;array_size;j++)
{
if(a[j]%26lt;a[min])
{
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment