Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.

Similar presentations


Presentation on theme: "Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search."— Presentation transcript:

1 Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search

2 Alan YorinksLecture 7 2 What is a List? • A list is a varying-length, linear collection of homogeneous elements. • linear means each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

3 Alan YorinksLecture Basic Kinds of ADT Operations • Constructor -- creates a new instance (object) of an ADT • Transformer -- changes the state of one or more of the data values of an instance • • Observer -- allows us to observe the state of one or more of the data values of an instance without changing them

4 Alan YorinksLecture 7 4 ADT List Operations • Transformers • Insert • Delete • SelSort • Observers • IsEmpty • IsFull • Length • IsPresent • Print change state observe state

5 Alan YorinksLecture 7 5 ADT Unsorted List Data Components ; length ; data[ 0.. MAX_LENGTH -1 ] number of elements in list array of list elements

6 Alan YorinksLecture 7 6 IsFull Length SelSort IsPresent Delete IsEmpty Insert Print Private data: length data [ 0 ] [ 1 ] [ 2 ] [MAX_LENGTH-1] Array-based class List

7 Alan YorinksLecture 7 7 ; // SPECIFICATION FILE ARRAY-BASED LIST( list.h ) ; const int MAX_LENGTH = 50 ; ; typedef int ItemType ; ; class List// declares a class data type ; { ; public : // public member functions ; List ( ) ;// constructor ; bool IsEmpty ( ) const ; ; bool IsFull ( ) const ; ; int Length ( ) const ; // returns length of list ; void Insert ( ItemType item ) ; ; void Delete ( ItemType item ) ; ; bool IsPresent( ItemType item ) const ; ; void SelSort ( ); ; void Print ( ) ; ; private :// private data members ; int length ; // number of values currently stored ; ItemTypedata[MAX_LENGTH] ; ; } ; 7 Array Based List Header

8 Alan YorinksLecture 7 8 Sorted and Unsorted Lists UNSORTED LIST Elements are placed into the list in no particular order. SORTED LIST List elements are in an order that is sorted in some way -- either numerically or alphabetically.

9 Alan YorinksLecture 7 9 ; // IMPLEMENTATION FILE ARRAY-BASED LIST ( list.cpp ) ; #include list.h ; #include ; using namespace std; ; int List :: Length ( ) const ; { ; return length ; ; } ; bool List :: IsFull ( ) const ; ; return ( length == MAX_LENGTH ) ; ; } Array Based List Implementation

10 Alan YorinksLecture 7 10 ; List :: List ( ) ; // Constructor ; { ; length = 0 ; ; } ; void List :: Insert ( ItemType item ) ; { ; data [ length ] = item ; ; length++ ; ; } Array Based List Implementation Con't

11 Alan YorinksLecture 7 11 Before Inserting 64 into an Unsorted List length 3 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ]. [MAX_LENGTH-1] The item will be placed into the length location, and length will be incremented. item 64

12 Alan YorinksLecture 7 12 After Inserting 64 into an Unsorted List length 4 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ] 64. [MAX_LENGTH-1] The item will be placed into the length location, and length will be incremented. item 64

13 Alan YorinksLecture 7 13 ; bool List :: IsEmpty ( ) const ; { ; return ( length == 0 ) ; ; } ; bool List :: IsPresent ( ItemType item ) const ; // Searches the list for item, reporting whether it was found ; { int index = 0 ; ; while ( index < length && item != data [ index ] ) ; index++ ; ; return ( index < length ) ; ; } Array Based List Implementation Con't

14 Alan YorinksLecture 7 14 ; void List :: Delete ( ItemType item ) ; //{ int index = 0 ; ; ; while ( index < length && item != data [ index ] ) ; index++; ; // if item found, move last element into items place ; if ( index < length ) ; { ; data [ index ] = data [length - 1 ] ; ; length-- ; ; } Array Based List Implementation Con't

15 Alan YorinksLecture 7 15 Deleting 39 from an Unsorted List index : 0 39 has not been matched. item 39 length 4 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ] [MAX_LENGTH-1]

16 Alan YorinksLecture 7 16 Deleting 39 from an Unsorted List Index : 1 item 39 length 4 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ] 64. [MAX_LENGTH-1] 39 has been matched.

17 Alan YorinksLecture 7 17 Deleting 39 from an Unsorted List index : 1 item 39 length 4 data [ 0 ] 15 [ 1 ] 64 [ 2 ] -90 [ 3 ] 64. [MAX_LENGTH-1] Placed copy of last list element into the position where 39 was before.

18 Alan YorinksLecture 7 18 Deleting 39 from an Unsorted List index : 1 item 39 length 3 data [ 0 ] 15 [ 1 ] 64 [ 2 ] -90 [ 3 ] 64. [MAX_LENGTH-1] Decremented length.

19 Alan YorinksLecture 7 19 ; void List :: Print ( ) ; { ; int index ; ; ; for ( index = 0 ; index < length ; index++ ) ; cout << data [ index ] << endl ; ; } Printing the List

20 Alan YorinksLecture 7 20 Selection Sort Process • examines the entire list to select the smallest element. Then places that element where it belongs (with array subscript 0) • examines the remaining list to select the smallest element from it. Then places that element where it belongs (with array subscript 1). • examines the last 2 remaining list elements to select the smallest one. Then places that element where it belongs in the array

21 Alan YorinksLecture 7 21 Selection Sort Algorithm ; FOR passCount going from 0 through length - 2 ; Find minimum value in data [ passCount.. length-1 ] ; Swap minimum value with data [ passCount ] ; length = 5 data [ 0 ] data [ 1 ] data [ 2 ] 6060 data [ 3 ] data [ 4 ] 8080 pass = 0

22 Alan YorinksLecture 7 22 ; void List :: SelSort ( ) ; // Sorts list into ascending order using selection sort ; { ItemType temp ; ; int passCount ; ; int sIndx ; ; int minIndx ; // index of minimum so far ; ; for ( passCount = 0 ; passCount < length - 1 ; passCount++ ) ; { ; minIndx = passCount ; ; // find index of smallest of data [ passCount.. length-1 ] ; for ( sIndx = passCount + 1 ; sIndx < length ; sIndx++ ) ; if ( data [ sIndx ] = data [ minIndx ] ) ; minIndx = sIndx ; ; temp = data [ minIndx ] ;// swap ; data [ minIndx ] = data [ passCount ] ; ; data [ passCount ] = temp ; ; } Selection Sort Algorithm

23 Alan YorinksLecture 7 23 IsFull Length IsPresent Delete IsEmpty Insert Print Private data: lengthdata [ 0 ] [ 1 ] [ 2 ] [MAX_LENGTH-1] SortedList Array-based class SortedList

24 Alan YorinksLecture 7 24 ; // SPECIFICATION FILE ARRAY-BASED SORTED LIST( slist.h ) ; const int MAX_LENGTH = 50 ; ; typedef int ItemType ; ; class SortedList ; { ; public : // public member functions ; SortedList ( ) ;// constructor ; bool IsEmpty ( ) const ; ; bool IsFull ( ) const ; ; int Length ( ) const ; // returns length of list ; void Insert ( ItemType item ) ; ; void Delete ( ItemType item ) ; ; bool IsPresent( ItemType item ) const ; ; void Print ( ) ; ; private :// private data members ; int length ; // number of values currently stored ; ItemTypedata[MAX_LENGTH] ; ; void BinSearch ( ItemType item, bool& found, int& position ) const ; ; } ; 24 Sorted List ADT

25 Alan YorinksLecture 7 25 Member Functions ; Which member function specifications and implementations must change to ensure that any instance of SortedList ADT remains sorted at all times? • Insert • Delete

26 Alan YorinksLecture 7 26 Insert Algorithm for SortedList ADT ; create space for the new item by shifting down all the larger list elements ; put the new item in the list ; increment length

27 Alan YorinksLecture 7 27 ; void SortedList :: Insert ( ItemType item ) ; { ; int index ; ; // find proper location for new element ; index = length - 1 ; ; // starting at bottom of array ; // shift down values larger than item ; // to make room for new item ; while ( index >= 0 && item < data [ index ] ) ; { ; data [ index + 1 ] = data [ index ] ; ; index-- ; ; } ; // insert item into array ; data [ index ] = item ; ; length++ ; ; } Sorted List Insert Implementation

28 Alan YorinksLecture 7 28 Delete Algorithm for SortedList ADT ; find the position of the element to be deleted from the sorted list ; eliminate space occupied by the item being deleted by shifting up all the larger list elements ; decrement length

29 Alan YorinksLecture 7 29 ; void SortedList :: Delete ( ItemType item ) ; { ; bool found ;// true, if item is found ; int position ;// position of item, if found ; int index ; ; // find location of element to be deleted ; BinSearch ( item, found, position) ; ; if ( found ) ; { ; // shift up elements that follow deleted item in sorted list ; ; for ( index = position ; index < length + 1 ; index++ ) ; data [ index ] = data [ index + 1 ] ; length -- ; ; } Sorted List Delete Implementation

30 Alan YorinksLecture 7 30 Improving Member Function IsPresent for SortedList ; Recall that with the unsorted List ADT ; we examined each list element beginning ; with data[ 0 ], until we either found a ; match with item, or we had examined all ; the elements in the unsorted List. ; How can the searching algorithm be improved for SortedList ADT?

31 Alan YorinksLecture 7 31 Searching for 55 in a SortedList Item 55 length 4 data [ 0 ] 15 [ 1 ] 39 [ 2 ] 64 [ 3 ] 90. [MAX_LENGTH-1] A sequential search for 55 can stop when 64 has been examined.

32 Alan YorinksLecture 7 32 Binary Search in SortedList ; Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array. ; Repeat the process in the half of the data that should be examined next. ; Stop when item is found, or when there is nowhere else to look and item has not been found.

33 Alan YorinksLecture 7 33 ; void SortedList::BinSearch ( ItemType item, bool& found, int& position ) ; // Searches sorted list for item, returning position of item, if item was found ; { ; int middle ; ; int first = 0; ; intlast = length - 1 ; ; found = false ; ; while ( last >= first && !found ) ; {middle = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT ; if ( item < data [ middle ] ) ; last = middle - 1 ; // LOOK IN FIRST HALF NEXT ; else if ( item > data [ middle ] ) ; first = middle + 1 ;// LOOK IN SECOND HALF NEXT ; else ; found = true ; // ITEM HAS BEEN FOUND ; } ; if ( found ) ; position = middle ; ; } Binary Search Implementation

34 Alan YorinksLecture 7 34 Trace of Binary Search ; data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Item = 84 first middle last data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first middle last item > data [ middle ] first = middle + 1 item < data [ middle ] last = middle - 1

35 Alan YorinksLecture 7 35 Trace continued ; data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Item = 84 first, last, middle data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last middle item == data [ middle ] found = true item > data [ middle ] first = middle + 1

36 Alan YorinksLecture 7 36 Another Binary Search Trace ; data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Item = 45 first middle last data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first middle last item < data [ middle ] last = middle - 1 item > data [ middle ] first = middle + 1

37 Alan YorinksLecture 7 37 Trace continued ; data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Item = 45 first, middle, last data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last middle item < data [ middle ] last = middle - 1 item > data [ middle ] first = middle + 1

38 Alan YorinksLecture 7 38 Trace concludes ; data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Item = 45 last first first > last found = false


Download ppt "Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search."

Similar presentations


Ads by Google