selection sorting s[] : array of int or float size: number of elements of s[] pseudocode for i = 0...

24
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

Upload: adam-walsh

Post on 17-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

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

Page 2: 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

Sorting Array of Objects

Student s[MAX_SIZE];

int size;

Sort students by what?

2

Page 3: 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

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

Page 4: 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

enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};struct Tdateclass Student

class StudentList{private: Student students[MAX_SIZE]; int numStudents;

public: StudentList() { numStudents = 0; } . . . void Sort() { . . . }};

4

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

Sorting Array of Objectsvoid 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

Page 10: 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

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

Page 11: 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

Selection Sortingvoid 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

Page 12: 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

Selection SortingWithout 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

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

Sorting Array of Objects

Sort Student by Age DOB (Ascending: youngest to oldest)

How to compare students on DOB?Use a (private) method!

18

Page 19: 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

Method YoungerThan

// Inside class Student

// if ( s1.YoungerThan(s2) )bool YoungerThan(const Student& s) { . . .)

19

Page 20: 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

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

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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

Page 24: 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

ScheduleLab 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 924