C Program to sort elements using SELECTION SORT

C Program for SELECRTION SORT: C programming code for SELECRTION SORT to sort numbers or arrange them in ascending order.

#include <stdio.h>
#include <conio.h>
int minpos(int [],int,int);
int main()
{
int n, j, pass, temp, min, arr[10],ptr;
printf(“*******Selection Sort*********nn”);
printf(“Enter the size of array : “);
scanf(“%d”,&n);

printf(“nEnter the element : n”);
for(int i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}

printf(“nOriginal array : “);

for(int i=0;i<n;i++)
{
printf(“%dt”,arr[i]);
}

for(pass=0;pass<n-1;pass++)
{
ptr=minpos(arr,pass,n);
temp=arr[pass];
arr[pass]=arr[ptr];
arr[ptr]=temp;

}

printf(“nnSorted elements : “);
for(int i=0;i<n;i++)
{
printf(“%dt”,arr[i])  ;
}

getch();

}

int minpos(int arr[],int x,int n)
{
int loc=x,min;
min=arr[x];

for(int i=x+1;i<n;i++)
{
if(arr[i]<min)
{
min=arr[i];
loc=i;

}

}
return loc;
}

OUTPUT

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s