Given an array of roll number and marks the task is to sort the roll numbers based on marks obtained in descending order.
Example: Consider an input list:-
| Roll | Marks |
| 1 | 343 |
| 2 | 356 |
| 5 | 370 |
| 3 | 330 |
| 8 | 300 |
The program should sort the marks in descending order and print the list as given below:-
| Roll | Marks |
| 5 | 370 |
| 2 | 356 |
| 1 | 343 |
| 3 | 330 |
| 8 | 300 |
Java
import java.util.*;
public class RollMarksSorting
{
public static void main(String args[])
{
int size=0,rTemp=0;
double mTemp=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of students: ");
size=sc.nextInt();
int roll[]=new int[size];
double marks[]=new double[size];
System.out.println("Enter Roll and Marks of the students:-");
for(int i=0;i<size;i++)
{
roll[i]=sc.nextInt();
marks[i]=sc.nextDouble();
System.out.println();
}
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-i-1;j++)
{
if(marks[j]<marks[j+1])
{
mTemp=marks[j];
marks[j]=marks[j+1];
marks[j+1]=mTemp;
rTemp=roll[j];
roll[j]=roll[j+1];
roll[j+1]=rTemp;
}
}
}
System.out.println("Sorted list:-");
System.out.println("Roll\t\tMarks");
for(int i=0;i<size;i++)
{
System.out.println(roll[i]+"\t\t"+marks[i]);
}
}
}Java