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.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient.
Advertisements

Slide 1 Insert your own content. Slide 2 Insert your own content.
Chapter 7: Arrays In this chapter, you will learn about
Linked Lists.
Linear Lists – Array Representation
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
ADTs unsorted List and Sorted List
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Review Pseudo Code Basic elements of Pseudo code
CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.
Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
Foundations of Data Structures Practical Session #11 Sort properties, Quicksort algorithm.
One Dimensional Arrays
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.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
1 CSE1301 Computer Programming: Lecture 28 List Sorting.
1 CSE1301 Computer Programming Lecture 32: List Sorting.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Searching and Sorting Arrays
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Building Java Programs Chapter 13 Searching reading: 13.3.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
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.
CSCI 51 Introduction to Programming March 12, 2009.
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.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
Data Structure and Algorithms
CSCI 51 Introduction to Programming March 10, 2009.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study.
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.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
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.
Data Structures I (CPCS-204)
Engineering Problem Solving with C++, Etter
CS 1430: Programming in C++.
Searching and Sorting Linear Search Binary Search ; Reading p
Selection Sort – an array sorting algorithm
Selection sort Given an array of length n,
Sorting Algorithms.
CMSC 202 Lesson 22 Templates I.
CS 1430: Programming in C++.
Ch. 12 Tables and Priority Queues
CS 1430: Programming in C++.
Arrays Part 2.
Templates I CMSC 202.
Chapter 2 : List ADT Part I – Sequential List
Sorting Algorithms.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Class StudentList class StudentList { private: int numStudents;
Sorting Algorithms.
Selection Sorting S[] : array of int or float
Presentation transcript:

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 values Only dynamic list: add, and delete an element 2

Add an object to the list Student stuList[MAX_STUDENTS]; int numStudents; numStudents = 4 MAX_STUDENTS = 10 stu int Add( const Student& stu ) { if ( Find( stu ) == -1 && numStudents < MAX_STUDENTS ) { stuList[numStudents] = stu ; numStudents ++; return 0; } return -1; } numStudents = 5 3

Delete an object from the list numStudents = 4 MAX_STUDENTS = 10 int Delete( long stuId ) { int index; index = Find( stuId ); if ( index != -1) { for ( int i = index; i < numStudents - 1; i++ ) stuList[i] = stuList[i+1]; numStudents --; return 0; } return -1; } // Delete the Student object // with ID 1002 numStudents = 3 4

Sorted List How to sort an unsorted list? Unsorted List: Sorted List (Ascending): Sorted List (Descending): 5

Selection Sort Example Find the smallest value between s[0] and s[7]: s[]: Exchange s[0] and s[2] 1 at index 2 Find the smallest value between s[1] and s[7]: Exchange s[1] and s[4] 3 at index Find the smallest value between s[2] and s[7]: Exchange s[2] and s[3] 5 at index Find the smallest value between s[3] and s[7]: Exchange s[3] and s[3] 9 at index

Selection Sort Example (cont.) Find the smallest value between s[4] and s[7]: Exchange s[4] and s[5] 10 at index Find the smallest value between s[5] and s[7]: Exchange s[5] and s[5] 12 at index Find the smallest value between s[6] and s[7]: Exchange s[6] and s[7] 15 at index

Pseudo Code for Selection Sort Given s[MAX_SIZE], size; for i = 0 to size - 2 find the index of a smallest element between s[i] and s[size - 1] swap s[i] and s[index] What functions do we need? IndexOfMin ( int begin, int end, const DataType s[] ) Swap ( int i, int j, DataType s[] ) 8

Sort Student List In StudentList class, we can have the following functions: void SortByID( ) { int indexLow; for ( int i = 0; i <= numStudents - 2; i++ ) { indexLow = IndexOfMinID( i, numStudents - 1 ); Swap( i, indexLow ); } Why don’t we need the Student object array as a parameter for IndexOfMinID and Swap? Because they are member functions of StudentList class! 9

IndexOfMinID function int IndexOfMinID( int begin, int end ) { int index = begin; for ( int i = begin + 1; i <= end; i ++ ) if ( stuList[i].LessThanByID( stuList[index] ) ) index = i; return index; } 10

Swap two elements in an array void Swap ( int i, int j ) { if ( i != j ) { Student stu = stuList[i]; stuList[i] = stuList[j]; stuList[j] = stu; } } // correct one! void Swap ( int i, int j ) { if ( i != j ) { stuList[i] = stuList[j]; stuList[j] = stuList[i]; } } // Is it correct? NO! both stuList[i] and stuList[j] will be the original stuList[j]! 11

Swap two variable values in general void Swap ( int num1, int num2 ) { if ( num1 != num2 ) { int temp = num1; num1 = num2; num2 = temp; } } // Is it correct? NO! The exchange does not remain after the function call, because num1 and num2 are IN parameters! void Swap ( int& num1, int& num2 ) { if ( num1 != num2 ) { int temp = num1; num1 = num2; num2 = temp; } } // Is it correct? 12

Insert an object to the sorted list numStudents = 4 MAX_STUDENTS = stu // Insert stu as the third // element of the list Pseudo code to insert to an ascending ordered list: Given s[MAX_SIZE], size; if the object to insert is not yet in s[] position = index of the first element larger than the object to insert for i = size-1 to position move s[i] to s[i+1] s[position] = object to insert size ++ 13

int Insert( const Student& stu ) { if ( Find( stu ) == -1 && numStudents < MAX_STUDENTS ) { int index = Position(stu); for ( int i = numStudents; i > index; i -- ) stuList[i] = stuList[i-1]; stuList[index] = stu; numStudents ++; return 0; } return -1; } int Position ( const Student& stu ) const { for ( int i = 0; i < numStudents; i ++ ) if ( stuList[i].GetID() > stu.GetID() ) return i; return numStudents; } Insert an object to the sorted list 14

Private or Public? In the StudentList class: void Read( int size ) void Print() const int Find( const Student& stu ) const int Find( long stuID ) const void UpdateGPA ( long id, float gpa ) void GetStats( float& max, float& min, float& average ) const int IndexOfMinID( int begin, int end ) void Swap ( int i, int j ) void SortByID( ) int Position ( const Student& stu ) int Add( const Student& stu ) int Delete( long stuId ) int Insert( const Student& stu ) // private 15