The document discusses abstract data types for unsorted and sorted lists, specifying their structures as array-based implementations and defining common operations like insert, delete, retrieve. Unsorted lists have no order between elements while sorted lists are organized by key values. Code implementations for unsorted and sorted list ADTs are provided with analysis of time complexity for each operation.
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Abstract Data Type - Unsorted and Sorted Lists (Array Implementation
1. Abstract Data Type - Unsorted List and Sorted
List (Array-based Implementation]
CSE225: Data Structures and
Algorithms
2. List Definitions
Linear relationship
Each element except the first has a unique predecessor, and
Each element except the last has a unique successor.
Length
The number of items in a list;
The length can vary over time.
2
3. 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
4. Data vs. Information
Data
• 6.34
• 6.45
• 6.39
• 6.62
• 6.57
• 6.64
• 6.71
• 6.82
• 7.12
• 7.06
SIRIUS SATELLITE RADIO INC.
$5.80
$6.00
$6.20
$6.40
$6.60
$6.80
$7.00
$7.20
1 2 3 4 5 6 7 8 9 10
Last 10 Days
Stock
Price
Information
6. 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
ID Name Address
Key
Sorted List
7. Abstract Data Type (ADT)
• A data type whose properties (domain and
operations) are specified independently of any
particular implementation.
7
8. What
How
Data from 3 different levels
• Application (or user) level:
modeling real-life data in a specific context.
• Logical (or ADT) level:
abstract view of the domain and operations.
• Implementation level:
specific representation of the structure to hold the data items, and the
coding for operations.
8
Why
9. 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.
10. 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.
11. 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.
12. 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.
13. 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.
14. 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.
15. unsortedtype.h
#ifndef UNSORTEDTYPE_H_INCLUDED
#define UNSORTEDTYPE_H_INCLUDED
const int MAX_ITEMS = 10;
template <class ItemType>
class UnsortedType
{
public :
UnsortedType();
void MakeEmpty();
bool IsFull();
int LengthIs();
void InsertItem(ItemType);
void DeleteItem(ItemType);
void RetrieveItem(ItemType&, bool&);
void ResetList();
void GetNextItem(ItemType&);
private:
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
#endif // UNSORTEDTYPE_H_INCLUDED
16. #ifndef SORTEDTYPE_H_INCLUDED
#define SORTEDTYPE_H_INCLUDED
const int MAX_ITEMS = 5;
template <class ItemType>
class SortedType
{
public :
SortedType();
void MakeEmpty();
bool IsFull();
int LengthIs();
void InsertItem(ItemType);
void DeleteItem(ItemType);
void RetrieveItem(ItemType&, bool&);
void ResetList();
void GetNextItem(ItemType&);
private:
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
#endif // SORTEDTYPE_H_INCLUDED
sortedtype.h
49. Retrieving an Item from Sorted List
• Find 73
• Step 1
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
50. Retrieving an Item from Sorted List
• Find 73
• Step 1
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
mid
=(0+14)/2
51. Retrieving an Item from Sorted List
• Find 73
• Step 1
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
mid
=(0+14)/2
52. Retrieving an Item from Sorted List
• Find 73
• Step 2
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
53. Retrieving an Item from Sorted List
• Find 73
• Step 2
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
mid
=(8+14)/2
54. Retrieving an Item from Sorted List
• Find 73
• Step 2
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
mid
=(8+14)/2
55. Retrieving an Item from Sorted List
• Find 73
• Step 3
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
56. Retrieving an Item from Sorted List
• Find 73
• Step 3
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
mid
=(8+10)/2
57. Retrieving an Item from Sorted List
• Find 73
• Step 3
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
mid
=(8+10)/2
58. Retrieving an Item from Sorted List
• Find 73
• Step 4
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
59. Retrieving an Item from Sorted List
• Find 73
• Step 4
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first last
mid
=(10+10)/2
60. Retrieving an Item from Sorted List
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
first
last
• Find 73
• Step 5
• last < first (indicates the absence of the item)
61. Retrieving an Item from Sorted List
Array size expressed as 2a
N 2(x)
N/2 2(x-1)
N/4 2(x-2)
N/8 2(x-3)
. .
. .
. .
. .
4 22
2 21
1 20
• What is the number of steps required?
62. Retrieving an Item from Sorted List
Array size expressed as 2a
N 2(x)
N/2 2(x-1)
N/4 2(x-2)
N/8 2(x-3)
. .
. .
. .
. .
4 22
2 21
1 20
𝟐𝒙
~𝑵
Or,
𝒙 = 𝐥𝐨𝐠𝟐 𝑵
Or simply,
𝒙 = 𝐥𝐨𝐠 𝑵
• What is the number of steps required?