Custom Search

Saturday 16 June 2012

Selection Sort Using JAVA


import java.io.*;
public class selectionsort
{
public static void main(String ar[])throws IOException
{
int a[]=new int[10];
int n,i,temp,loc,min,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter the limit: ");
n=Integer.parseInt(br.readLine());
System.out.print("enter the elements: ");
for(i=0;i
{
a[i]=Integer.parseInt(br.readLine());
}
System.out.print("sorted array is:\n");
for(i=0;i
{
min=a[i];
loc=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]>min)
{
min=a[j];
loc=j;
}
}
if(loc!=i)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
for(i=0;i
{
System.out.print(a[i]);
System.out.print("\t");
}
}
}

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...