Unsorted Sorted List_Array (1)
Unsorted Sorted List_Array (1)
2
List Definitions
Unsorted list
A list in which data items are placed in no particular order;
the only relationship between data elements is the list
predecessor and successor relationships.
Sorted list
A list that is sorted by the value in the key;
There is a semantic relationship among the keys of the
items in the list.
Key
The attributes that are used to determine the logical order
of the list.
3
Data vs. Information
Data Information
• 6.34 SIRIUS SATELLITE RADIO INC.
• 6.45 $7.20
• 6.39 $7.00
• 6.62 $6.80
• 6.57
Stock Price
$6.60
• 6.64 $6.40
• 6.71 $6.20
• 6.82 $6.00
• 7.12 $5.80
1 2 3 4 5 6 7 8 9 10
• 7.06
Last 10 Days
Unsorted List Sorted List
22 12
12 14
46 22
35 35
14 46
. .
. .
. .
. .
Sorted List
ID Name Address
22 Jack Black 120 S. Virginia Street
45 Simon Graham 6762 St Petersburg
59 Susan O'Neal 1807 Glenwood, Palm Bay
66 David peterson 1207 E. Georgetown
Key
Abstract Data Type (ADT)
7
Data from 3 different levels
How
• Implementation level:
specific representation of the structure to hold the data items, and the
coding for operations.
8
Specification of UnsortedType
Structure: The list has a special property called the current position - the
position of the last element accessed by GetNextItem during an
iteration through the list. Only ResetList and GetNextItem affect
the current position.
Operations (provided by Unsorted List ADT):
MakeEmpty
Function Initializes list to empty state.
Precondition
Postcondition List is empty.
Boolean IsFull
Function Determines whether list is full.
Precondition List has been initialized.
Postcondition Returns true if list is full and false otherwise.
Specification of UnsortedType
int LengthIs
Function Determines the number of elements in list.
Precondition List has been initialized.
Postcondition Returns the number of elements in list.
RetrieveItem (ItemType &item, Boolean &found)
Function Retrieves list element whose key matches item's key (if present).
Precondition List has been initialized. Key member of item is initialized.
Postcondition If there is an element someItem whose key matches item's key,
then found = true and item is a copy of someItem; otherwise
found = false and item is unchanged. List is unchanged.
InsertItem (ItemType item)
Function Adds item to list.
Precondition List has been initialized. List is not full. item is not in the list.
Postcondition item is in the list. List is changed.
Specification of Unsorted Type
DeleteItem (ItemType item)
Function Deletes the element whose key matches item's key.
Precondition List has been initialized. Key member of item is initialized. One
and only one element in list has a key matching item's key.
Post-condition No element in list has a key matching item's key.
ResetList
Function Initializes current position for an iteration through the list.
Precondition List has been initialized.
Post-condition Current position is prior to first element in list.
GetNextItem (ItemType &item)
Function Gets the next element in list.
Precondition List has been initialized. Current position is defined. Element at
current position is not last in list.
Post-condition Current position is updated to next position. item is a copy of
element at current position.
Specification of SortedType
Structure: The list has a special property called the current position - the
position of the last element accessed by GetNextItem during an
iteration through the list. Only ResetList and GetNextItem affect
the current position.
Operations (provided by Unsorted List ADT):
MakeEmpty
Function Initializes list to empty state.
Precondition
Post-condition List is empty.
Boolean IsFull
Function Determines whether list is full.
Precondition List has been initialized.
Post-condition Returns true if list is full and false otherwise.
Specification of SortedType
int LengthIs
Function Determines the number of elements in list.
Precondition List has been initialized.
Postcondition Returns the number of elements in list.
RetrieveItem (ItemType& item, Boolean& found)
Function Retrieves list element whose key matches item's key (if present).
Precondition List has been initialized. Key member of item is initialized.
Postcondition If there is an element someItem whose key matches item's key,
then found = true and item is a copy of someItem; otherwise
found = false and item is unchanged. List is unchanged.
InsertItem (ItemType item)
Function Adds item to list.
Precondition List has been initialized. List is not full. item is not in list.
Postcondition item is in list. List is still sorted.
Specification of SortedType
DeleteItem (ItemType item)
Function Deletes the element whose key matches item's key.
Precondition List has been initialized. Key member of item is initialized. One
and only one element in list has a key matching item's key.
Postcondition No element in list has a key matching item's key. List is still
sorted.
ResetList
Function Initializes current position for an iteration through the list.
Precondition List has been initialized.
Postcondition Current position is prior to first element in list.
GetNextItem (ItemType& item)
Function Gets the next element in list.
Precondition List has been initialized. Current position is defined. Element at
current position is not last in list.
Postcondition Current position is updated to next position. item is a copy of
element at current position.
unsortedtype.h
#ifndef UNSORTEDTYPE_H_INCLUDED
#define UNSORTEDTYPE_H_INCLUDED
[0] 6 [0] 6
[1] 3 [1] 3
[2] 4 [2] 4
[3] 1 [3] 1
[4] 2 [4] 2
. [5] 5
. .
. Logical .
garbage Logical
. . garbage
[MAX_ITEMS - 1] [MAX_ITEMS - 1]
length = 5 length = 6
Insert 5
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item)
{
info[length] = item;
length++;
}
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item)
{
info[length] = item;
}
length++;
O(1)
Inserting an Item into Sorted List
[0] 1 [0] 1
[1] 2 [1] 2
[2] 4 [2] 4
[3] 6 [3] 5
[4] 8 [4] 6
. [5] 8
. .
. Logical .
garbage Logical
. . garbage
[MAX_ITEMS - 1] [MAX_ITEMS - 1]
length = 5 length = 6
Insert 5
sortedtype.cpp
template <class ItemType>
void SortedType<ItemType>::InsertItem(ItemType item)
{
int location = 0;
bool moreToSearch = (location < length);
while (moreToSearch)
// This will identify the location where the item will be stored
{
if(item > info[location])
{
location++;
moreToSearch = (location < length);
}
else if(item < info[location])
moreToSearch = false;
}
for (int index = length+1; index > location; index--)
info[index] = info[index - 1];
info[location] = item;
length++;
}
sortedtype.cpp
template <class ItemType>
void SortedType<ItemType>::InsertItem(ItemType item)
{
int location = 0;
bool moreToSearch = (location < length);
while (moreToSearch)
{
O(N)
if(item > info[location])
{
location++;
moreToSearch = (location < length);
}
else if(item < info[location])
O(N)
moreToSearch = false;
}
for (int index = length; index > location; index--)
info[index] = info[index - 1];
info[location] = item;
length++;
O(N)
}
Deleting an Item from Unsorted List
[0] 6 [0] 6
[1] 3 [1] 3
[2] 4 [2] 4
[3] 1 [3] 5
[4] 2 [4] 2
[5] 5 .
. .
. Logical
. Logical garbage
. garbage .
[MAX_ITEMS - 1] [MAX_ITEMS - 1]
length = 6 length = 5
Delete 1
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
[0] 1 [0] 1
[1] 2 [1] 2
[2] 4 [2] 4
[3] 5 [3] 6
[4] 6 [4] 8
[5] 8 .
. .
. . Logical
Logical garbage
. garbage .
[MAX_ITEMS - 1] [MAX_ITEMS - 1]
length = 6 length = 5
Delete 5
sortedtype.cpp
template <class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 1
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 1
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 1
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 3
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
=(8+10)/2
• Step 3
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
=(8+10)/2
• Step 3
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 4
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
=(10+10)/2
• Step 4
• 84 found at the midpoint
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 1
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 1
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 1
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 3
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
=(8+10)/2
• Step 3
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
=(8+10)/2
• Step 3
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 4
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
=(10+10)/2
• Step 4
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
last first
• Step 5
• last < first (indicates the absence of the item)
Retrieving an Item from Sorted List
• What is the number of steps required?