Selection Sort > C Program

Analysis of Algorithm

Selection Sort > C Program


#include<stdio.h>
#include<conio.h>
void select(int x[],int n);
void main()
{

int arr[20],i,n;
clrscr();
printf("Enter the no. of elements in the array :");
scanf("%d",&n);
printf("Enter the elements in the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
select(arr,n);
printf("The sorted array is :\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
getch();
}
void select(int x[],int n)
{
int i,j,max,index;
for(i=n-1;i>=1;i--)
{
max=x[0];
index=0;
for(j=1;j<=i;j++)
if(x[j]>max)
{
max=x[j];
index=j;
}
x[index]=x[i];
x[i]=max;
}
}

Comments

Popular posts from this blog

Intermediate Code Generation > C Program