Selection Sorting S[] : array of int or float

Slides:



Advertisements
Similar presentations
Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
Advertisements

1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
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 Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
CS 1430: Programming in C++.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
COMPUTER 2430 Object Oriented Programming and Data Structures I
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
CS150 Introduction to Computer Science 1
Sorting Algorithms.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
null, true, and false are also reserved.
Object-Oriented Programming (OOP) Lecture No. 36
Introduction to Programming
Chapter 18-3 Recursion Dale/Weems.
Sorting Algorithms.
Working With Arrays.
CS 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Searching and Sorting Arrays
Introduction to Programming
Search,Sort,Recursion.
Standard Input/Output Stream
CS 1430: Programming in C++.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Chapter 9: Data Structures: Arrays
CS150 Introduction to Computer Science 1
Searching and Sorting Arrays
CS150 Introduction to Computer Science 1
Sorting Algorithms.
Class StudentList class StudentList { private: int numStudents;
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Programming with Arrays 2
Structure (i.e. struct) An structure creates a user defined data type
CSCE 206 Lab Structured Programming in C
Priority Queue and Heap
CS100A Sections Dec Loop Invariant Review C Review and Example
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
CS 1430: Programming in C++.
Presentation transcript:

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]

Sorting Array of Objects Student s[MAX_SIZE]; int size; Sort students by what?

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

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() };

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]

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]); } };

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;

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; }

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;

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);

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;

Without Private Methods 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;

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!

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;

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()) s1.getLast() == s2.getLast() s1.getFirst() < s2.getFirst()) else return false; } // Correct?

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) strcmp(last1, last2) == 0 && strcmp(first1, first2) < 0) else return false; }

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) ); }

Sorting Array of Objects Sort Student by Age DOB (Ascending: youngest to oldest) How to compare students on DOB? Use a (private) method!

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

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

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) DOB.month == s.DOB.month && DOB.day > s.DOB.day) else return false; )

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; )

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) ); )

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