Selection Sort - Java
import java.util.*;
class Selsort
{
int a[]=new int[50];
int i,n,j;
Scanner s=new Scanner(System.in);
void read()
{
System.out.println("Enter number of elements...");
n=s.nextInt();
System.out.println("Enter Elements.....");
for(i=0;i<n;i++)
a[i]=s.nextInt();
}
void sort()
{
for(i=0;i<n-1;i++)
{
int p=i;
for(j=i+1;j<n;j++)
if(a[p]>a[j]) p=j;
if(p!=i) { int t=a[p];a[p]=a[i];a[i]=t;}
}
}
void disp()
{
System.out.println("Sorted Array is..............");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
class Main
{
public static void main(String args[])
{
Selsort ss=new Selsort();
ss.read();
ss.sort();
ss.disp();
}
}
Comments
Post a Comment