⏹⏹⏺⏺write a program to input 40 words in an array arrange these words in descending order of alphabets using selection Sort technique in the sorted array⏺⏺⏹⏹
Answers
Answer:
import java.util.Scanner;
public class JavaExample
{
public static void main(String[] args)
{
int count;
String temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of strings you would like to enter:");
count = scan.nextInt();
String str[] = new String[count];
Scanner scan2 = new Scanner(System.in);
System.out.println("Enter the Strings one by one:");
for(int i = 0; i < count; i++)
{
str[i] = scan2.nextLine();
}
scan.close();
scan2.close();
//Sorting the strings
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++) {
if (str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.print("Strings in Sorted Order:");
for (int i = 0; i <= count - 1; i++)
{
System.out.print(str[i] + ", ");
}
}
}
Explanation:
code in java:
import java.util.Scanner;
public class Alphabetical_Order
{
public static void main(String[] args)
{
int n, tempi;
String temps;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
int weights[] = new int[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
System.out.println("Enter all the weights:");
for(int i = 0; i < n; i++)
{
weights[i] = s1.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temps = names[i];
names[i] = names[j];
names[j] = temps;
tempi = weights[i];
weights[i] = weights[j];
weights[j] = tempi;
}
}
}
System.out.println("Names in Sorted Order:");
for (int i = 0; i <= n - 1; i++)
{
System.out.println(names[i] + " - "+ weights[i]);
}
}
}