Write a program to create two SDA to store names and marks of 25 students. Then display the name of the students as per their marks in descending order using bubble sorting.
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
String names[] = new String[25];
int marks[] = new int[25];
for (int i = 0; i < 25; i++) {
System.out.print("Enter the name " + (i + 1) + ": ");
names[i] = keyBoard.nextLine();
System.out.print("Enter the mark " + (i + 1) + ": ");
marks[i] = keyBoard.nextInt();
keyBoard.nextLine();
}
bubbleSort(names, marks);
System.out.printf("%-15s%-15s\n", "Name", "Mark");
for (int i = 0; i < 25; i++) {
System.out.printf("%-15s%-15d\n", names[i], marks[i]);
}
keyBoard.close();
}
static void bubbleSort(String names[], int numbers[]) {
int n = numbers.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (numbers[j] < numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
String tempName = names[j];
names[j] = names[j + 1];
names[j + 1] = tempName;
}
}
}
Comments
Leave a comment