Download presentation
Presentation is loading. Please wait.
Published byAdam Walsh Modified over 9 years ago
1
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] 1
2
Sorting Array of Objects Student s[MAX_SIZE]; int size; Sort students by what? 2
3
const int MAX_NAME_SIZE = 15; const int ID_SIZE = 2; enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; struct TDate { int year, month, day; // default is public }; class Student { private: char id[ID_SIZE + 1], firstName[MAX_NAME_SIZE + 1], lastName[MAX_NAME_SIZE + 1]; float gpa; TDate DOB; Status standing; public:... }; 3
4
enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; struct Tdate class Student class StudentList { private: Student students[MAX_SIZE]; int numStudents; public: StudentList() { numStudents = 0; }... void Sort() {... } }; 4
5
Sorting Array of Objects Sort Student by gpa in Ascending Order for i = 0 to size - 2 find the index of a student with the lowest gpa between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] 5
6
class StudentList { private: Student students[MAX_SIZE]; int numStudents; void SwapTwoStudents(Student& s1, Student& s2) int IndexOfMinGPA(int first, int last) public: void SortStudentOnGPA() { int index; for (int i = 0; i < numStudents – 1; i ++) { index = IndexOfMinGPA(i, numStudents – 1); if (index != i) SwapTwoStudents(students[i], students[index]); } } }; 6
7
Method IndexOfMinGPA Student students[MAX_SIZE]; int numStudents; int IndexOfMinGPA(int first, int last) { int index = first; for (int i = first + 1; i <= last; i++) { if (students[i].getGPA() < students[index].getGPA()) index = i; } return index; } 7
8
Method SwapTwoStudent //-------------------------------------------------- // The function exchanges two objects of Student. // Parameters: ( InOut, InOut ) //-------------------------------------------------- void SwapTwoStudents(Student& x, Student& y) { Student temp = x; x = y; y = temp; } 8
9
Sorting Array of Objects void SwapTwoStudents(Student& x, Student& y); int IndexOfMinGPA(int first, last); //-------------------------------------------------- // The method uses Selection Sorting // to sort array students[] of Student on GPA // in non-descending order //-------------------------------------------------- void SortStudentOnGPA() { int index; for (int i = 0; i < numStudents - 1; i++) { index = IndexOfMinGPA(i, numStudents - 1); if (index != i) SwapStudent(students[i], students[index]); } return; } 9
10
Sorting Array of Objects //-------------------------------------------------- // The method uses Selection Sorting // to sort array students[] of Student on GPA // in non-ascending order: How to modify it? //-------------------------------------------------- void SortStudentOnGPA() { int index; for (int i = 0; i < numStudents - 1; i++) { index = IndexOfMaxGPA(i, numStudents - 1); if (index != i) SwapStudent(students[i], students[index]); } return; } void SwapTwoStudent(Student& x, Student& y); int IndexOfMaxGPA(int first, last); 10
11
Selection Sorting void SwapTwoStudent(Student& x, Student& y); int IndexOfMinGPA(int first, last); //-------------------------------------------------- // The method uses Selection Sorting // to sort array students[] of Student on GPA // in non-ascending order. // How to do it without new method? //-------------------------------------------------- void SortStudentOnGPA() { int index; for (int i = numStudents - 1; i > 0; i--) { index = IndexOfMinGPA(0, i); if (index != i) SwapStudent(students[i], students[index]); } return; } 11
12
Selection Sorting Without Private Methods void SortStudentOnGPA() { for (int i = numStudents - 1; i > 0; i--) { int index = 0; for (int j = 1; j <= i; j ++) if (students[j].getGPA() < students[index].getGPA()) index = i; if (index != i) { Student temp = students[i]; students[i] = students[index]; students[index] = temp; } return; } 12
13
Sorting Array of Objects Sort Student by GPA (Descending) and Name (lastName & firstName Ascending) for i = 0 to numStudents - 2 find the index of the required student between s[i] and s[numStudents - 1] If i not the same as index swap s[i] and s[index] How to find the required student? Use a private method! 13
14
Method IndexOfTheStudent //----------------------------------------------------------------- // The method finds and returns the index of the // student who has the highest GPA between s[first] and s[last]; // if two or more students have the same highest GPA, it returns // the index of student with the smallest name (lastName then // firstName). // Parameters: (In, In) //----------------------------------------------------------------- int IndexOfTheStudent(int first, int last) { int index = first; for (int i = first + 1; i <= last; i++) { // How to compare two students on GPA and name // Call method CompGPA_Name()! if ( CompGPA_Name(students[i], students[index]) ) index = i; } return index; } 14
15
Method CompGPA_Name() //---------------------------------------------------- // The method compares two students, and returns true // if s1 has higher GPA, or s1 has the same GPA as s2 // and has a smaller name (last then first). // It returns false otherwise. // Parameters: ( In, In) //---------------------------------------------------- bool CompGPA_Name(const Student& s1, const Student& s2) { if (s1.getGPA() > s2.getGPA()) return true; else if (s1.getGPA() == s2.getGPA() && s1.getLast() < s2.getLast()) return true; else if (s1.getGPA() == s2.getGPA() && s1.getLast() == s2.getLast() s1.getFirst() < s2.getFirst()) return true; else return false; } // Correct? 15
16
Method CompGPA_Name() bool CompGPA_Name(const Student& s1, const Student& s2) { char first1[MAX + 1], first2[MAX + 1], last1[MAX + 1], last2[MAX + 1]; s1.getFirst(first1); s1.getLast(last1); s2.getFirst(first2); s2.getLast(last2); if (s1.getGPA() > s2.getGPA()) return true; else if (s1.getGPA() == s2.getGPA() && strcmp(last1, last2) < 0) return true; else if (s1.getGPA() == s2.getGPA() && strcmp(last1, last2) == 0 && strcmp(first1, first2) < 0) return true; else return false; } 16
17
Method CompGPA_Name() bool CompGPA_Name(const Student& s1, const Student& s2) { char first1[MAX + 1], first2[MAX + 1], last1[MAX + 1], last2[MAX + 1]; s1.getFirst(first1); s1.getLast(last1); s2.getFirst(first2); s2.getLast(last2); return ( (s1.getGPA() > s2.getGPA()) || (s1.getGPA() == s2.getGPA() && strcmp(last1, last2) < 0) || (s1.getGPA() == s2.getGPA() && strcmp(last1, last2) == 0 && strcmp(first1, first2) < 0) ); } 17
18
Sorting Array of Objects Sort Student by Age DOB (Ascending: youngest to oldest) How to compare students on DOB? Use a (private) method! 18
19
Method YoungerThan // Inside class Student // if ( s1.YoungerThan(s2) ) bool YoungerThan(const Student& s) {... ) 19
20
const int MAX_NAME_SIZE = 15; const int ID_SIZE = 2; enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; struct TDate { int year, month, day; // default is public }; class Student { private: char id[ID_SIZE + 1], firstName[MAX_NAME_SIZE + 1], lastName[MAX_NAME_SIZE + 1]; float gpa; TDate DOB; Status standing; public:... }; 20
21
struct TDate { int year, month, day; // default is public }; class Student { private: TDate DOB;... // if ( s1.YoungerThan(s2) ) bool YoungerThan(const Student& s) { if (DOB.year > s.DOB.year) return true; else if (DOB.year == s.DOB.year && DOB.month > s.DOB.month) return true; else if (DOB.year == s.DOB.year && DOB.month == s.DOB.month && DOB.day > s.DOB.day) return true; else return false; ) }; 21
22
struct TDate { int year, month, day; // default is public }; class Student { private: TDate DOB;... // if ( s1.YoungerThan(s2) ) bool YoungerThan(const Student& s) { if ( (DOB.year > s.DOB.year) || (DOB.year == s.DOB.year && DOB.month > s.DOB.month) || (DOB.year == s.DOB.year && DOB.month == s.DOB.month && DOB.day > s.DOB.day) ) return true; else return false; ) }; 22
23
struct TDate { int year, month, day; // default is public }; class Student { private: TDate DOB;... // if ( s1.YoungerThan(s2) ) bool YoungerThan(const Student& s) { return ( (DOB.year > s.DOB.year) || (DOB.year == s.DOB.year && DOB.month > s.DOB.month) || (DOB.year == s.DOB.year && DOB.month == s.DOB.month && DOB.day > s.DOB.day) ); ) }; 23
24
Schedule Lab 10 Grace Time: 5 PM, Today Lab 11 Due Time: 5 PM, Thursday Prog6 Due Time: 9:30 PM, Wednesday, May 11 Grace Time: 9:30 PM, Friday, May 13 Test 3 Monday, May 9 24
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.