W.A.P. in JAVA to sort array in alphabetical order.
Answers
/**
* This is JAVA program to implement selection sorting method
*/
import java.util.*;
class alpha_sort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int l=5,id; String sm,temp;
System.out.println("Enter five names : ");
String [] s= new String[l];
for(int i=0;i<l;i++)
{
s[i]=sc.nextLine();
}
for(int i=0;i<l;i++)
{
sm=s[i];
id=i;
for(int j=i+1;j<l;j++)
{
if(s[j].compareTo(sm)<0)
{
sm=s[j];
id=j;
}
}
temp=s[i];
s[i]=s[id];
s[id]=temp;
}
System.out.println("Sorted Names : ");
for(int i=0;i<l;i++)
{
System.out.print(s[i]+" ");
}
}
}
Answer:
Heyy buddy, here's ur answer
Refer to the attachment below
Hope it helps