logo

Sorting Students by Last Name

   

Added on  2019-09-23

9 Pages1038 Words149 Views
 | 
 | 
 | 
Solution 1public class Student {publicStringfirstName;publicStringlastName;publicint id;publicStudent(StringfirstName,StringlastName,int id) { this.firstName=firstName; this.lastName=lastName; this.id=id; }}public class BubbleSort {// bubble sort method to sort according to last namepublic void bubbleSort(Student[] s) {for (int i = 0; i <s.length; i++) {for (int j = i + 1; j < s.length; j++) {if ((s[i].lastName.compareTo(s[j].lastName)) > 0) {String firstName=s[j].firstName;String lastName= s[j].lastName;int id=s[j].id;s[j].firstName=s[i].firstName;s[j].lastName=s[i].lastName;s[j].id=s[i].id;s[i].firstName=firstName;s[i].lastName=lastName;s[i].id=id;}}}for (Student stu : s) {System.out.println(stu.firstName + " " + stu.lastName + " " + stu.id);}}}
Sorting Students by Last Name_1

public class InsertionSort {// insertionSort method to sort according to last namepublic void insertionSort(Student[] s) { int n =s.length; for (int j = 1; j < n; j++) { String key = s[j].lastName; String key1 = s[j].firstName; int key2 = s[j].id; int i = j-1; while ( (i > -1) && ( s[i].lastName.compareTo(key) >0 ) ) {s[i+1].firstName=s[i].firstName;s[i+1].lastName=s[i].lastName;s[i+1].id=s[i].id; i--; } s[i+1].lastName = key; s[i+1].firstName=key1; s[i+1].id=key2;} for (Student stu : s) {System.out.println(stu.firstName + " " + stu.lastName + " " + stu.id);}}}public class SelectionSort {// selectionSort method to sort according to last namepublic void selectionSort(Student[] s) {for (int i = 0; i < s.length - 1; ++i) {int minIndex = i;for (int j = i + 1; j < s.length; ++j) {if (s[j].lastName.compareTo(s[minIndex].lastName) < 0) {minIndex = j;}String firstName = s[i].firstName;String lastName = s[i].lastName;
Sorting Students by Last Name_2

int id= s[i].id;s[i].firstName = s[minIndex].firstName;s[i].lastName = s[minIndex].lastName;s[i].id = s[minIndex].id;s[minIndex].firstName = firstName;s[minIndex].lastName = lastName;s[minIndex].id = id;}}for (Student stu : s) {System.out.println(stu.firstName + " " + stu.lastName + " " + stu.id);}}}public class NamesMain {public static void main(String[] args) {// TODO Auto-generated method stubStudent[] s = new Student[8];s[0] = new Student("Saoud", "Mohamed", 3);s[1] = new Student("Abdelkader", "Farouk", 2);s[2] = new Student("Beshr", "Alsharqawy", 1);s[3] = new Student("Nader", "Salah", 4);s[4] = new Student("Basem", "Hawary", 5);s[5] = new Student("Abdullah", "Babaker", 6);s[6] = new Student("Abdelaal", "Khairy", 7);s[7] = new Student("Mohamedain", "Marsily", 8); //Unsorted Array System.out.println("Unsorted by the last name ");for (Student stu : s) {System.out.println(stu.firstName + " " + stu.lastName + " " + stu.id);} System.out.println();System.out.println("------------------------Bubble Sort-----------------------");
Sorting Students by Last Name_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents