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.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

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.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
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.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
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
Chapter 9 Data Structures: Arrays and Structs Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
Searching Arrays Linear search Binary search small arrays
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
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.
11/26/2015 CSI Chapter 10, Part I 1 Introducing Arrays… An array is a collection of sequentially indexed elements, each element having the same.
Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration.
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!
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
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.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 1 void GetAllData(float a[][MAX_COLS],
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.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
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.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Default Arguments.
CS 1430: Programming in C++.
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.
CSCE 206 Lab Structured Programming in C
CS 1430: Programming in C++.
Selection Sorting S[] : array of int or float
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] 1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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