1 / 17

Selection Sorting

Selection Sorting. Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] -----. Selection Sorting. Pseudocode (Backward) for i = size – 1 to 1

Download Presentation

Selection Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] -----

  2. Selection Sorting Pseudocode (Backward) for i = size – 1 to 1 find the index of the required element between s[0] and s[i] If i not the same as index swap s[i] and s[index] -----

  3. Sorting Array of Structs Sort Student by GPA (Descending) and ID (Ascending) StudentType s[MAX_SIZE]; int size; for i = 0 to size - 2 find the index of the required student between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] -----

  4. Function IndexOfTheStudent // The function 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, return // the index of student with the smallest ID. // Parameters: (In, In, In) int IndexOfTheStudent(const StudentType s[], int first, int last) { int index = first; for (int i = first + 1; i <= last; i++) { // How to compare two students on GPA and ID if ( ) index = i; } return index; } -----

  5. Function CompGPA_ID // The function compares two students: // It returns true if s1 has higher GPA, or // s1 has the same GPA as s2 but has a smaller ID // It returns false otherwise. // Parameters: ( In, In) bool CompGPA_ID(const StudentType& s1, const StudentType& s2) { if (s1.gpa > s2.gpa) return true; else if (s1.gpa == s2.gpa && s1.id < s2.id) return true; else return false; } -----

  6. Function CompGPA_ID // The function compares two students: // It returns true if s1 has higher GPA, or // s1 has the same GPA as s2 but has a smaller ID // It returns false otherwise. // Parameters: ( In, In) bool CompGPA_ID(const StudentType& s1, const StudentType& s2) { if (s1.gpa > s2.gpa || (s1.gpa == s2.gpa && s1.id < s2.id)) return true; else return false; } -----

  7. Function CompGPA_ID // The function compares two students: // It returns true if s1 has higher GPA, or // s1 has the same GPA as s2 but has a smaller ID // It returns false otherwise. // Parameters: ( In, In) bool CompGPA_ID(const StudentType& s1, const StudentType& s2) { return ((s1.gpa > s2.gpa) || (s1.gpa == s2.gpa && s1.id < s2.id)); } -----

  8. Function IndexOfTheStudent // The function 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, return // the index of student with the smallest ID. // Parameters: ( In, In, In) int IndexOfTheStudent(const StudentType s[], int first, int last) { int index = first; for (int i = first + 1; i <= last; i++) { // How to compare two students on GPA and ID if (CompGPA_ID(s[i], s[index])) index = i; } return index; } -----

  9. Sorting Students on Two Fields // The function uses Selection Sorting method // to sort an array of struct StudentType on GPA in // ascending order and then on ID in descending order. // Parameters: ( InOut , In ) void SortStudentArray(StudentType s[], int size) { int index; for (int i = 0; i < size - 1; i++) { index = IndexOfTheStudent(s, i, size - 1); if (index != i) SwapInt(s[i], s[index]); } return; } -----

  10. Struct SectionType { int numStudents; StudentType students[30]; }; int main() { SectionType CS143; GetSection(CS143); cout << "The students before sorting: "; DisplaySection(CS143); SortStudentArray(CS143.students, CS143.numStudents); cout << "The students after sorting: "; DisplaySection(CS143); return 0; } -----

  11. Function Younger // The function compares two students: // It returns true if s1 younger than s2 // It returns false otherwise. // Parameters: ( In, In) bool Younger(const StudentType& s1, const StudentType& s2) { if ((s1.DOB.year > s2.DOB.year) || (s1.DOB.year == s2.DOB.year && s1.DOB.month > s2.DOB.month) || (s1.DOB.year == s2.DOB.year && s1.DOB.month == s2.DOB.month && s1.DOB.day > s2.DOB.day)) return true; else return false; ) -----

  12. Function After // The function compares two dates: // It returns true if date1 after date2 // It returns false otherwise. // Parameters: ( In, In) bool After(const TDate& date1, const TDate& date2) { if ((date1.year > date2.year) || (date1.year == date2.year && date1.month > date2.month) || (date1.year == date2.year && date1.month == date2.month && date1.day > date2.day)) return true; else return false; ) -----

  13. Function After // The function compares two dates: // It returns true if date1 after date2 // It returns false otherwise. // Parameters: ( In, In) bool After(const TDate& date1, const TDate& date2) { return ((date1.year > date2.year) || (date1.year == date2.year && date1.month > date2.month) || (date1.year == date2.year && date1.month == date2.month && date1.day > date2.day)); } -----

  14. Function Younger bool After(const TDate& date1, const TDate& date2); // The function compares two students: // It returns true if s1 younger than s2 // It returns false otherwise. // Parameters: ( In, In) bool Younger(const StudentType& s1, const StudentType& s2) { if (After(s1.DOB, s2.DOB)) return true; else return false; ) -----

  15. Function Younger bool After(const TDate& date1, const TDate& date2); // The function compares two students: // It returns true if s1 younger than s2 // It returns false otherwise. // Parameters: ( In, In) bool Younger(const StudentType& s1, const StudentType& s2) { return After(s1.DOB, s2.DOB); ) -----

  16. Other Sorting Algorithms • Bubble Sorting • Insertion Sorting • Merge Sorting • Heap Sorting • Quick Sorting • Others

  17. Quiz 9 – Part III • 2 points • Do it in HiC • Submit to HiC server as Quiz93 • Due Monday, by 5 PM -----

More Related