WAP IN JAVA TO SORT AN GIVEN ELEMENT IN AN ARRAY USING SELECTION SORT
import java.util.*;
class selection
{
public static void main(String args[])
{
int n,t,j,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.of element");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the array element");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
System.out.println("ARRAY element in sorted order");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
output:
Enter the no.of element
5
Enter the array element
88
1
0
6
3
ARRAY element in sorted order
0
1
3
6
88
class selection
{
public static void main(String args[])
{
int n,t,j,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.of element");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the array element");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
System.out.println("ARRAY element in sorted order");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
output:
Enter the no.of element
5
Enter the array element
88
1
0
6
3
ARRAY element in sorted order
0
1
3
6
88
Comments
Post a Comment