C++/Java Bubble Sort You\'ll be given a list of students, and you\'ll need to so
ID: 3749119 • Letter: C
Question
C++/Java Bubble Sort
You'll be given a list of students, and you'll need to sort it using bubble sort. We want to sort the students according to graduation year (soonest first), and for students with the same graduation year, alphabetically by first name (and if there are any students with the same first name and graduation year, ties should be broken by last name). However, rather than just printing the final sorted list, you'll print the partially sorted list after each pass (equivalently, after the largest remaining element bubbles to the end).
Input Format
The first line of the input consists of an integer, n, indicating the number of words you'll need to sort. The next n lines will each consist of student data wtih space-separated first name, last name, and graduation year, in that order. The input will terminate with a blank line. For example:
Constraints
You can assume n is a non-negative integer. You can further assume the list contains no students with the same first name, last name, and graduation year.
Output Format
You will be printing the partially sorted list n - 1 times (the last time will be entirely sorted). Each student should appear on a new line without terminating whitespace, space separated in the same format it was provided in the input, and each partially sorted list should be followed by a blank line to indicate where it ends and the next one begins. For example:
Sample Input 0
Sample Output 0
Explanation / Answer
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) {
//input
Scanner in = new Scanner(System.in);
int n =Integer.parseInt(in.nextLine());
Student students[] = new Student[n];
for(int i=0;i<n;i++){
String s = in.nextLine();
System.out.println(s);
String inputs[] = s.split(" ");
students[i] = new Student(inputs[0],inputs[1],Integer.parseInt(inputs[2]));
}
//bubble sort
Student temp;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(StudentComparator.compare(students[j],students[j+1])>0){
temp=students[j];
students[j]=students[j+1];
students[j+1]=temp;
//print
System.out.println("");
for(int k=0;k<n;k++){
students[k].printStudent();
}
}
}
}
}
}
class Student{
public String first_name;
public String last_name;
public int graduation_year;
public Student(String first_name,String last_name,int graduation_year){
this.first_name = first_name;
this.last_name = last_name;
this.graduation_year = graduation_year;
}
public void printStudent(){
System.out.println(this.first_name+" "+this.last_name+" "+String.valueOf(this.graduation_year));
}
}
class StudentComparator{
static int compare(Student s1,Student s2){
if(s1.graduation_year<s2.graduation_year){
return -1;
}else if(s1.graduation_year>s2.graduation_year){
return 1;
}else{
int cmp = s1.first_name.compareTo(s2.first_name);
if(cmp!=0){
return cmp;
}else{
return s1.last_name.compareTo(s2.last_name);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.