Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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.
Answer:
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
// Accept 40 words
Scanner scanner = new Scanner(System.in);
String[] words = new String[40];
System.out.println("Enter 40 words: ");
for (int i = 0; i < 40; i++) {
words[i] = scanner.nextLine();
}// Sort using selection sort
for (int i = 0; i < words.length - 1; i++) {
int largestWordIndex = i;
for (int j = i + 1; j < words.length; j++) { 20 if (words[j].compareTo(words[largestWordIndex]) > 0) {
largestWordIndex = j;
}
}
String temp = words[largestWordIndex];
words[largestWordIndex] = words[i];
words[i] = temp;
}
// Print sorted array
for (int i = 0; i < 40; i++) {
System.out.println(words[i]);
}
}
}
C. Sir| Created: 15-Apr-2019 | Updated: 15-Apr-2019|ICSE2017