Title: Lecture 7 ADT Sorted List, Binary Search
1Lecture 7ADT Sorted List, Binary Search
Logical level
ID Goods Quantity 23 C book
10 12 beer can 100 45 blank cd
30 32 pair of socks 40
12 beer can 10023 C book 1032
pair of socks 4045 blank cd
30
- Difference between unsortedand sorted lists
- Sorted lists have an additional property the
key member of any item (except the first one)
comes before the key member of the next one. - They both provide the same operations, but the
operations provided by sorted lists for which
order is relevant are specified differently.
2- ADT Operations
- Constructors
- language-level declaration
- Observers
- IsFull ? bool
- LengthIs ? int
- RetrieveItem(item)?item
- Transformers
- MakeEmpty
- InsertItem(item)
- DeleteItem(item)
- Iterators
- ResetList
- GetNextItem ? item
Function Adds item to
list.Preconditions List has been initialized.
List is not full.
item is not in the list.
List is sorted by key
member. Postconditions item is in list.
List is still sorted
Function Deletes the element whose key matches
items key. Preconditions List has been
initialized. Key member of item is
initialized. List is sorted by key member.
One and only one element in the list has a key
matching items key. Postconditions No element
in list has key matching items key. List is
still sorted.
3Specification for ADT Sorted List (part 1)
ifndef SORTED_TYPE_H define SORTED_TYPE_H incl
ude ltstringgt define MAX_ITEMS 100 struct
ItemType int ID // the key field string
productName int quantity
SortedType.h
4Specification for ADT Sorted List (part 2)
class SortedTypepublic // Constructor
SortedType() // Transformers void
MakeEmpty() void InsertItem(ItemType item)
void DeleteItem(ItemType item) // Observers
bool IsFull() const int LengthIs() const
void RetrieveItem(ItemType item, bool found)
// Iterators void ResetList() void
GetNextItem(ItemType item) private //here
come the data members endif
SortedType.h
5Implementation of ADT Sorted List (or how ADT
Sorted List looks inside)
Internal Data Structure
int length ItemType infoMAX_ITEMS int
currentPos
6Implementation of ADT Sorted List (more)
Implementation of the member function DeleteItem
void SortedTypeDeleteItem(ItemType item) //
Pre items key has been initialized one and
only // one element in the list has a key
that matches // items the list elements
are sorted by key // Post No element in the list
has a key that matches // items the list
is still sorted by key int location 0
while (infolocation.ID ! item.ID)
location for(int i location i lt length-2
i) infoi infoi1 length--
Deleting 58
7
length 8
120
7Implementation of ADT Sorted List (more)
Implementation of the member function InsertItem
void SortedTypeInsertItem(ItemType item)
int i, location 0 bool moreToSearch
(location lt length) while (moreToSearch)
if (infolocation.ID gt item.ID) moreToSearch
false else location moreToSearch
(location lt length) for(ilength i
gt location i--) infoi infoi-1
infolocation item length
Inserting 58
length 7
8
58
8Implementation of ADT Sorted List (more)
Improving the implementation of the RetrieveItem
operation
Binary Search
Pages 1 .. 400
201
200
Searching for page 274
1..400
300
301
201..400
250
251
201..299
275
274
251..299
9Implementation of ADT Sorted List (more)
Binary Search based implementation of
RetrieveItem
void SortedTypeRetrieveItem(ItemType
item,bool found) int midPoint, first 0,
last length - 1 bool moreToSearch (first
lt last) found false while (moreToSearch
!found) midPoint (first last) / 2
if (item.ID infomidPoint.ID) found
true item infolocation else if
(item.ID lt infomidPoint.ID) last midPoint
1 moreToSearch (first lt last)
else if (item.ID gt infomidPoint.ID) first
midPoint 1 moreToSearch (first lt last)
//end while
10Binary Search Example
Searching 120
first 0 last 9 midPoint (0 9) / 2 4
11Binary Search Example
found
Searching 120
first 5 last 9 midPoint (5 9) / 2 7
12Binary Search Example
Searching 49
first 0 last 9 midPoint (0 9) / 2 4
13Binary Search Example
Searching 49
first 0 last 3 midPoint (0 3) / 2 1
14Binary Search Example
Searching 49
first 2 last 3 midPoint (2 3) / 2 2
15Binary Search Example
Searching 49
not found
first 2 last 1 (first gt last) gt item not
found
16Excercises
Problem 1. Modify the member function
SortedTypeDeleteItem to satisfy the following
specification void SortedTypeDeleteItem(ItemTy
pe item) // Pre items key has been
initialized // there might be 0, 1 or
more elements in // the list with a key
that matches // items key the list is
sorted. // Post No element in the list has a key
// that matches items the list is
still// sorted. Problem 2. Write a C
function MergeLists(SortedType l1, SortedType
l2, SortedType result) which merges two sorted
lists l1 and l2 into a third sorted list, which
is stored in the output parameter-variable
result.