Lecture09

Lecture09 - CMPSC 24: Lecture 9 Abstract Data Type: Lists...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 24: Lecture 9 Abstract Data Type: Lists Divyakant Agrawal Department of Computer Science UC Santa Barbara 4/25/10 1 Lecture Plan •  Array Based implementaIon of sorted list –  Time Complexity Analysis of OperaIons on Lists –  Can we do beLer? •  Linked List implementaIon of Sorted List –  Time Complexity of OperaIons –  Can we use the opImizaIon technique here? 2 ADT Sorted List •  Transformers –  MakeEmpty –  InsertItem –  DeleteItem change state •  Observers –  IsFull –  GetLength •  observe state –  RetrieveItem •  Iterators –  ResetList –  GetNextItem process all 3 Member funcIons Which member funcCon specificaCons and implementaCons must change? –  InsertItem –  DeleteItem 4 InsertItem for SortedList ADT (array) •  Find proper locaCon for the new element in the sorted list. •  Create space for the new element by moving down all the list elements that will follow it. •  Put the new element in the list. •  Increment length. 5 Array ImplementaIon •  InsertItem Ini$alize loca$on to posi$on of first item Set moreToSearch to (have not examined Info(last)) while moreToSearch switch (item.ComparedTo(Info(loca$on))) case LESS : Set moreToSearch to false case EQUAL : // Cannot happen case GREATER : Set loca$on to Next(loca$on) Set moreToSearch to (have not examined Info(last)) for index going from length DOWNTO loca$on + 1 Set Info(index ) to Info(index‐1) Set Info(loca$on) to item Increment length Why can't EQUAL happen? 6 Array ImplementaIon void SortedType :: InsertItem ( ItemType item ) { bool moreToSearch ; int location = 0 ; // find proper location for new element moreToSearch = ( location < length ) ; while ( moreToSearch ) { switch ( item.ComparedTo( info[location] ) ) { case LESS : moreToSearch = false ; break ; case GREATER : location++ ; moreToSearch = ( location < length ) ; break ; } } // make room for new element in sorted list for ( int index = length ; index > location ; index-- ) info [ index ] = info [ index - 1 ] ; info [ location ] = item ; length++ ; } 7 DeleteItem for SortedList ADT (array) •  Find the locaIon of the element to be deleted from the sorted list. •  Eliminate space occupied by the item being deleted by moving up all the list elements that follow it. •  Decrement length. 8 Array ImplementaIon • DeleteItem Ini$alize loca$on to posi$on of first item Set found to false while NOT found switch (item.ComparedTo(Info(loca$on))) case GREATER : Set loca$on to Next(loca$on) case LESS : // Cannot happen case EQUAL : Set found to true for index going from loca$on +1 TO length ‐1 Set Info(index ‐ 1) to Info(index) Decrement length Why can't LESS happen? 9 Array ImplementaIon void SortedType :: DeleteItem ( ItemType item ) { int location = 0 ; // find location of element to be deleted while ( item.ComparedTo ( info[location] ) != EQUAL ) location++ ; // move up elements that follow deleted item in sorted list for (int index = location + 1 ; index < info [ index - 1 ] = info [ index ] ; length-- ; length; index++) } 10 Pointer‐based ImplementaIon of InsertItem •  Let's see Set loca$on to listData Set moreToSearch to (loca)on != NULL) while moreToSearch switch (item.ComparedTo(loca)on‐>info)) case GREATER : Set loca$on to loca)on‐>next Set moreToSearch to (loca)on != NULL) case LESS : Set moreToSearch to false See the problem ? 11 Pointer‐based ImplementaIon We need a trailing pointer 12 InserIng ‘S’ into a Sorted List predLoc locaIon Private data: length 3 listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch 13 Finding proper posiIon for ‘S’ predLoc locaIon NULL Private data: length 3 listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch true 14 Finding proper position for ‘S’ predLoc locaIon Private data: length 3 listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch true 15 Finding Proper Position for ‘S’ predLoc locaIon Private data: length 3 listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch false 16 Inserting ‘S’ into Proper Position predLoc locaIon Private data: length listData currentPos 4 ‘C’ ‘L’ ‘X’ ‘S’ moreToSearch false 17 DeleteItem for SortedList ADT (pointers) •  Any changes? 18 How to improve searching in sorted lists? •  Binary search 19 Binary Seach in a Sorted List •  Examine 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 list that should be examined next. •  Stop when item is found, or when there is nowhere else to look. 20 void SortedType::RetrieveItem ( ItemType& { int midPoint ; int first = 0; int last = length - 1 ; item, bool& found ) found = false ; while (( first <= last ) && !found ) { midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT switch ( item.ComparedTo( info [ midPoint ] ) ) { case LESS : . . . // LOOK IN FIRST HALF NEXT case GREATER : . . . // LOOK IN SECOND HALF NEXT case EQUAL : . . . // ITEM HAS BEEN FOUND } } } 21 Trace of Binary Search item = 45 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last LESS last = midPoint ‐ 1 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last GREATER first = midPoint + 1 22 Trace conInued item = 45 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last midPoint GREATER first = midPoint + 1 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, midPoint, last LESS last = midPoint ‐ 1 23 Trace concludes item = 45 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] last first first > last loop terminates 24 void SortedType::RetrieveItem ( ItemType& item, bool& found ) // ASSUMES info ARRAY SORTED IN ASCENDING ORDER { int midPoint ; int first = 0; int last = length - 1 ; found = false ; //1 while (( first <= last ) && !found ) { midPoint = ( first + last ) / 2 ; //2 switch ( item.ComparedTo( info [ midPoint ] ) ) { case LESS : last = midPoint - 1 ; break ; case GREATER : first = midPoint + 1 ; break ; case EQUAL : found = true ; item = info[ midPoint ] ; break ; } } } 25 Loop Invariant J •  0 <= first <= last+1 <= length ˄ found → (length > 0 ˄ item = info[(first+last)/2] ) ˄ ¬found → ( item not in info[0 .. first‐1] ˄ K item not in info[last+1 .. length‐1] ) •  At 1:: 0 <= first <= last <= length‐1 ˄ J ˄ K •  At 2:: 0 <= first <= midpoint <= last <= length‐1 ˄ J ˄ K •  LESS:: 0 <= first <= (midpoint‐1) + 1 <= length ˄ item not in info[midpoint .. length‐1] ) •  GREATER:: 0 <= midpoint +1 <= last + 1 <= length ˄ item not in info[0 .. midpoint] 26 ...
View Full Document

This note was uploaded on 12/27/2011 for the course CMPSC 24 taught by Professor Agrawal during the Fall '09 term at UCSB.

Ask a homework question - tutors are online