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.

Slides:



Advertisements
Similar presentations
Recursion.
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.
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.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
Sorting Algorithms Insertion and Radix Sort. Insertion Sort One by one, each as yet unsorted array element is inserted into its proper place with respect.
Searching Arrays Linear search Binary search small arrays
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
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.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
CS 1430: Programming in C++.
Data Types Storage Size Domain of all possible values Operations 1.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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.
Starting Out with C++, 3 rd Edition 1 Sorting Arrays.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Data Structures I (CPCS-204)
Chapter 9: Searching, Sorting, and Algorithm Analysis
Merging Merge. Keep track of smallest element in each sorted half.
Sorting Algorithms.
Engineering Problem Solving with C++, Etter
Alg2_1c Extra Material for Alg2_1
Sorting Algorithms.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
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};
Sorting Lesson Outline
Chapter 18-3 Recursion Dale/Weems.
Sorting Algorithms.
CS 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Searching and Sorting Arrays
C++ Plus Data Structures
Search,Sort,Recursion.
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Search,Sort,Recursion.
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.
Insertion Sort Array index Value Insertion sort.
CS 1430: Programming in C++.
Stacks, Queues, ListNodes
Sorting Algorithms.
Selection Sorting S[] : array of int or float
Presentation transcript:

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 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] -----

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] -----

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

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

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

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

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

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

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

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

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

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

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

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

Other Sorting Algorithms Bubble Sorting Insertion Sorting Merge Sorting Heap Sorting Quick Sorting Others

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