Advertisements
Advertisements
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.
Solution
import java.util.Scanner;
class Descending {
public static void main(String args[ ]) {
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print(“Enter number of words you want to enter:”);
n = s.nextlnt( );
String names[ ] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println(“Enter all words:”);
for(int i = 0; i < n; i+ +) {
names[i] = s1.nextLine[( );
}
for (int i = 0; i < n; i+ +){
for (int j = i + 1; j < n;j++){
if (names[i].compareTo(namesG]) < 0) {
temp = names [i];
names[i] = namesG];
names[j] = temp;
}
}
}
System.out.print(“Words in Descending Order:”);
for (int i = 0;i<n-l;i++){
System.out.print(names[i] + “,”);
}
System.out.print(names[n – 1]);
}
}
APPEARS IN
RELATED QUESTIONS
What is the difference between the linear search and the binary search technique?
Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.
Name the following :
(i) A keyword used to call a package in the program.
(ii) Any one reference data type.
Differentiate between searching and sorting.
If int x [ ] = { 4, 3,7, 8, 9,10}; what are the values of p and q ?
(i) p = x.length
(ii) q = x[2] + x[5] * x[1]
Consider the given array and answer the question given below:
int x[ ] {4; 7,9,66,72,0,16);
What is the length of the array?
Consider the given array and answer the question given below:
int x[ ] {4; 7,9,66,72,0,16);
What is the value in x[4]?
Define a class to accept 10 characters from a user. Using bubble sort technique arrange them in ascending order. Display the sorted array and original array.
Define a class to accept values into an array of double data type of size 20. Accept a double value from user and search in the array using linear search method. If value is found display message "Found" with its potiition where it is present in the array. Otherwise display message "not found".
Define a class to search for a value input by the user from the list of values given below. If it is found display the message "Search successful", otherwise display the message "Search element not found” using Binary search technique.
5.6, 11.5, 20.8, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.