21BCE9726 - Experiment No 4.2
21BCE9726 - Experiment No 4.2
21BCE9726 - Experiment No 4.2
Aim: Write a program that takes the details of Students (name, roll number,
address, CGPA) and sort it in a non-decreasing order using Selection sort based on
CGPA.
Code:
import java.io.*;
sum += grade[i];
cgpa = sum / n;
return cgpa;
for(int i=0;i<n;i++) {
System.out.printf("%-10s|%-12s|%.2f\n",a[i],b[i],c[i]);
21BCE9726
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(bi.readLine());
for(int i=0;i<n;i++) {
names[i]=bi.readLine();
regd[i]=bi.readLine();
for(int j=0;j<5;j++) {
marks[i][j]=Integer.parseInt(input[j]);
for(int i=0;i<n;i++) {
cgpa[i]=cgpacalc(marks[i],5);
System.out.println("before sorting");
print(names,regd,cgpa,n);
//selection sort
int remaining=n;
for(int i=0;i<n;i++) {
21BCE9726
double max=0;
int max_index=0;
for(int j=0;j<remaining;j++) {
if(cgpa[j]>max) {
max=cgpa[j];
max_index=j;
Double temp1=cgpa[remaining-1];
cgpa[remaining-1]=max;
cgpa[max_index]=temp1;
String temp2=names[remaining-1];
names[remaining-1]=names[max_index];
names[max_index]=temp2;
String temp3=regd[remaining-1];
regd[remaining-1]=regd[max_index];
regd[max_index]=temp3;
remaining--;
System.out.println("After sorting");
print(names,regd,cgpa,n);
OUTPUT:
21BCE9726
21BCE9726