Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1

2 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

3 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

4 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

5 Sorted List How to sort an unsorted list? 9 9 12 1 1 5 5 3 3 10 21 15 1 1 3 3 5 5 9 9 10 12 15 21 Unsorted List: Sorted List (Ascending): 21 15 12 10 9 9 5 5 3 3 1 1 Sorted List (Descending): 5

6 Selection Sort Example 9 9 12 1 1 5 5 3 3 10 21 15 Find the smallest value between s[0] and s[7]: s[]: 1 1 12 9 9 5 5 3 3 10 21 15 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 4 1 1 3 3 9 9 5 5 12 10 21 15 Find the smallest value between s[2] and s[7]: Exchange s[2] and s[3] 5 at index 3 1 1 3 3 5 5 9 9 12 10 21 15 Find the smallest value between s[3] and s[7]: Exchange s[3] and s[3] 9 at index 3 1 1 3 3 5 5 9 9 12 10 21 15 6

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

8 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

9 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

10 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

11 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

12 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

13 Insert an object to the sorted list 1 1 3 3 5 5 numStudents = 4 MAX_STUDENTS = 10 4 4 stu 10 5 5 // 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

14 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

15 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


Download ppt "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."

Similar presentations


Ads by Google