Question
Write a program to input forty words in an array. Arrange these words in descending order of alphabets, using selection sort technique. Print the sorted array.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program9
{
public static void main(String[] args)
{
int i=0,j=0,largestWordPos=0;
String temp="";
Scanner sc = new Scanner(System.in);
String[] wd = new String[40];
System.out.println("Enter 40 words: ");
for (i = 0; i < 40; i++)
{
wd[i] = sc.next();
}
for (i = 0; i < wd.length - 1; i++)
{
largestWordPos = i;
for (j = i + 1; j < wd.length; j++)
{
if (wd[j].compareTo(wd[largestWordPos]) > 0)
{
largestWordPos = j;
}
}
temp = wd[largestWordPos];
wd[largestWordPos] = wd[i];
wd[i] = temp;
}
/* Print the sorted array*/
System.out.println("Word Array After Sorting");
for (i = 0; i < 40; i++)
{
System.out.println(wd[i]);
}
}
}