Tweet
public class selsort
{
public static void selsort(int a[],int n)
{
int i,j,min,pos=0;
for(i=0;i<n;i++)
{
min=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
pos=j;
}
}
int temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
public static void main(String args[])
{
System.out.println("Program to sort an array using selection sort : ");
int a[]={23,-34,68,35,-21,99,45,65,81,26};
int n=10,i;
System.out.println("\n\nGiven array : ");
for(i=0;i<n;i++)
System.out.print(a[i]+ " ");
selsort(a,n);
System.out.println("\n\n");
System.out.println("Sorted array : ");
for(i=0;i<n;i++)
System.out.print(a[i]+ " ");
System.out.println();
}
}
Selection Sort in Java
Posted by
LAHAUL SETH
~
Selection Sort in Java
2011-11-29T11:07:00+05:30
LAHAUL SETH
Java
|
Sorting
|
Comments
Selection Sort in Java
2011-11-29T11:07:00+05:30
LAHAUL SETH
Java
|
Sorting
|