Skip to main content

Write a program to accept 5 student names and their marks. Display the names in the descending order of the marks using bubble sort algorithm.

Write a program to accept 5 student names and their marks. Display the names in the descending order of the marks using bubble sort algorithm.
Input:
Ravi 25
Sheetal 30
Rajeev 27
Karan 28
Utsav 24
Output:
Sheetal 30
Karan 28
Rajeev 27
Ravi 25
Utsav 24


import java.util.Scanner;

class meritlist

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

String names[] = new String[5];

int marks[] = new int[5];

System.out.println("Enter 5 names and their marks:");

for(int i=0;i<5;i++)

{

System.out.println("Enter name:");

names[i]=sc.next();

System.out.println("Enter marks:");

marks[i]=sc.nextInt();

}

for(int i=0;i<5-1;i++)

{

for(int j=0;j<5-i-1;j++)

{

if(marks[j]<marks[j+1]) //for ascending order marks[j]>marks[j+1]

{

//marks sorting descending order 

int t = marks[j];

marks[j=marks[j+1];

marks[j+1]=t;

//name sorting 

String temp = names[j];

names[j]=names[j+1];

names[j+1]=temp;

}

}

}

System.out.println("Merit List is");

System.out.println("Names\tMarks");

for(int i=0;i<5;i++)

{

System.out.println(names[i]+"\t"+marks[i]);

}

}

}

Popular posts from this blog