SlideShare a Scribd company logo
1 of 64
Abstract Data Type - Unsorted List and Sorted
List (Array-based Implementation]
CSE225: Data Structures and
Algorithms
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
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
• 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
22
12
46
35
14
.
.
.
.
Unsorted List
12
14
22
35
46
.
.
.
.
Sorted List
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
Abstract Data Type (ADT)
• A data type whose properties (domain and
operations) are specified independently of any
particular implementation.
7
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
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
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
#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
unsortedtype.cpp
#include “unsortedType.h"
template <class ItemType>
UnsortedType<ItemType>::UnsortedType()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
void UnsortedType<ItemType>::MakeEmpty()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
bool UnsortedType<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int UnsortedType<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void UnsortedType<ItemType>::ResetList()
{
currentPos = -1;
}
template <class ItemType>
void UnsortedType<ItemType>::GetNextItem(ItemType&
item)
{
currentPos++;
item = info [currentPos] ;
}
unsortedtype.cpp
#include “unsortedType.h"
template <class ItemType>
UnsortedType<ItemType>::UnsortedType()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
void UnsortedType<ItemType>::MakeEmpty()
{
length = 0;
}
template <class ItemType>
bool UnsortedType<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int UnsortedType<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void UnsortedType<ItemType>::ResetList()
{
currentPos = -1;
}
template <class ItemType>
void UnsortedType<ItemType>::GetNextItem(ItemType&
item)
{
currentPos++;
item = info [currentPos] ;
}
O(1)
O(1)
O(1)
O(1)
O(1)
O(1)
sortedtype.cpp
#include "sortedtype.h"
template <class ItemType>
SortedType<ItemType>::SortedType()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
void SortedType<ItemType>::MakeEmpty()
{
length = 0;
}
template <class ItemType>
bool SortedType<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int SortedType<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void SortedType<ItemType>::ResetList()
{
currentPos = -1;
}
template <class ItemType>
void SortedType<ItemType>::GetNextItem(ItemType&
item)
{
currentPos++;
item = info [currentPos];
}
sortedtype.cpp
#include "sortedtype.h"
template <class ItemType>
SortedType<ItemType>::SortedType()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
void SortedType<ItemType>::MakeEmpty()
{
length = 0;
}
template <class ItemType>
bool SortedType<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int SortedType<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void SortedType<ItemType>::ResetList()
{
currentPos = -1;
}
template <class ItemType>
void SortedType<ItemType>::GetNextItem(ItemType&
item)
{
currentPos++;
item = info [currentPos];
}
O(1)
O(1)
O(1)
O(1)
O(1)
O(1)
Inserting an Item into Unsorted List
[0] 6
[1] 3
[2] 4
[3] 1
[4] 2
.
.
.
.
[MAX_ITEMS - 1]
[0] 6
[1] 3
[2] 4
[3] 1
[4] 2
[5] 5
.
.
.
[MAX_ITEMS - 1]
Logical
garbage Logical
garbage
length = 5 length = 6
Insert 5
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item)
{
info[length] = item;
length++;
}
unsortedtype.cpp
O(1)
template <class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item)
{
info[length] = item;
length++;
}
Inserting an Item into Sorted List
[0] 1
[1] 2
[2] 4
[3] 6
[4] 8
.
.
.
.
[MAX_ITEMS - 1]
[0] 1
[1] 2
[2] 4
[3] 5
[4] 6
[5] 8
.
.
.
[MAX_ITEMS - 1]
Logical
garbage Logical
garbage
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)
{
if(item > info[location])
{
location++;
moreToSearch = (location < length);
}
else if(item < info[location])
moreToSearch = false;
}
for (int index = length; index > location; index--)
info[index] = info[index - 1];
info[location] = item;
length++;
}
O(N)
O(N)
O(N)
Deleting an Item from Unsorted List
[0] 6
[1] 3
[2] 4
[3] 1
[4] 2
[5] 5
.
.
.
[MAX_ITEMS - 1]
[0] 6
[1] 3
[2] 4
[3] 5
[4] 2
.
.
.
.
[MAX_ITEMS - 1]
Logical
garbage
Logical
garbage
length = 6 length = 5
Delete 1
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
while (item != info[location])
location++;
info[location] = info[length - 1];
length--;
}
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
while (item != info[location])
location++;
info[location] = info[length - 1];
length--;
} O(1)
O(N)
O(N)
Deleting an Item from Sorted List
[0] 1
[1] 2
[2] 4
[3] 5
[4] 6
[5] 8
.
.
.
[MAX_ITEMS - 1]
[0] 1
[1] 2
[2] 4
[3] 6
[4] 8
.
.
.
.
[MAX_ITEMS - 1]
Logical
garbage
Logical
garbage
length = 6 length = 5
Delete 5
sortedtype.cpp
template <class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
while (item != info[location])
location++;
for (int index = location + 1; index < length; index++)
info[index - 1] = info[index];
length--;
}
sortedtype.cpp
template <class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
while (item != info[location])
location++;
for (int index = location + 1; index < length; index++)
info[index - 1] = info[index];
length--;
}
O(N)
O(N)
O(N)
Retrieving an Item from Unsorted List
• Visit each element in the list, one by one, until the item is
found.
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool &found)
{
int location = 0;
bool moreToSearch = (location < length);
found = false;
while (moreToSearch && !found)
{
if(item == info[location])
{
found = true;
item = info[location];
}
else
{
location++;
moreToSearch = (location < length);
}
}
}
unsortedtype.cpp
template <class ItemType>
void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool &found)
{
int location = 0;
bool moreToSearch = (location < length);
found = false;
while (moreToSearch && !found)
{
if(item == info[location])
{
found = true;
item = info[location];
}
else
{
location++;
moreToSearch = (location < length);
}
}
}
O(N)
Retrieving an Item from Sorted List
• Find 84
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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• 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
Retrieving an Item from Sorted List
• Find 84
• Step 4
• 84 found at the midpoint
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
Retrieving an Item from Sorted List
• Find 73
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
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
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
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
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
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
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
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
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
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
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
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
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)
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?
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?
template <class ItemType>
void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found)
{
int midPoint, first = 0, last = length - 1;
bool moreToSearch = (first <= last);
found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
if(item < info[midPoint])
{
last = midPoint - 1;
moreToSearch = (first <= last);
}
else if(item > info[midPoint])
{
first = midPoint + 1;
moreToSearch = (first <= last);
}
else
{
found = true;
item = info[midPoint];
}
}
}
sortedtype.cpp
sortedtype.cpp
O(logN)
template <class ItemType>
void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found)
{
int midPoint, first = 0, last = length - 1;
bool moreToSearch = (first <= last);
found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
if(item < info[midPoint])
{
last = midPoint - 1;
moreToSearch = (first <= last);
}
else if(item > info[midPoint])
{
first = midPoint + 1;
moreToSearch = (first <= last);
}
else
{
found = true;
item = info[midPoint];
}
}
}

More Related Content

Similar to Abstract Data Type - Unsorted and Sorted Lists (Array Implementation

Linked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdfLinked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdfadityacomputers001
 
Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3Hariz Mustafa
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docxajoy21
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfxlynettalampleyxc
 
C++ detyrat postim_slideshare
C++ detyrat postim_slideshareC++ detyrat postim_slideshare
C++ detyrat postim_slidesharetctal
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfmallik3000
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfalivaisi1
 
Below is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdfBelow is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdfshalins6
 
Given the class specification of Unsorted Type as below- class Unsorte.docx
Given the class specification of Unsorted Type as below- class Unsorte.docxGiven the class specification of Unsorted Type as below- class Unsorte.docx
Given the class specification of Unsorted Type as below- class Unsorte.docxfrancescaj1
 
Prompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfPrompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfalsofshionchennai
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptxASRPANDEY
 

Similar to Abstract Data Type - Unsorted and Sorted Lists (Array Implementation (20)

강의자료10
강의자료10강의자료10
강의자료10
 
Linked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdfLinked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdf
 
Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docx
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
 
C++ detyrat postim_slideshare
C++ detyrat postim_slideshareC++ detyrat postim_slideshare
C++ detyrat postim_slideshare
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdf
 
Chap10
Chap10Chap10
Chap10
 
Below is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdfBelow is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdf
 
Given the class specification of Unsorted Type as below- class Unsorte.docx
Given the class specification of Unsorted Type as below- class Unsorte.docxGiven the class specification of Unsorted Type as below- class Unsorte.docx
Given the class specification of Unsorted Type as below- class Unsorte.docx
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Linked list1
Linked list1Linked list1
Linked list1
 
set.pptx
set.pptxset.pptx
set.pptx
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Prompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfPrompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdf
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
arrays in c
arrays in carrays in c
arrays in c
 
List
ListList
List
 

Recently uploaded

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 

Recently uploaded (20)

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
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
  • 17. unsortedtype.cpp #include “unsortedType.h" template <class ItemType> UnsortedType<ItemType>::UnsortedType() { length = 0; currentPos = -1; } template <class ItemType> void UnsortedType<ItemType>::MakeEmpty() { length = 0; currentPos = -1; } template <class ItemType> bool UnsortedType<ItemType>::IsFull() { return (length == MAX_ITEMS); } template <class ItemType> int UnsortedType<ItemType>::LengthIs() { return length; } template <class ItemType> void UnsortedType<ItemType>::ResetList() { currentPos = -1; } template <class ItemType> void UnsortedType<ItemType>::GetNextItem(ItemType& item) { currentPos++; item = info [currentPos] ; }
  • 18. unsortedtype.cpp #include “unsortedType.h" template <class ItemType> UnsortedType<ItemType>::UnsortedType() { length = 0; currentPos = -1; } template <class ItemType> void UnsortedType<ItemType>::MakeEmpty() { length = 0; } template <class ItemType> bool UnsortedType<ItemType>::IsFull() { return (length == MAX_ITEMS); } template <class ItemType> int UnsortedType<ItemType>::LengthIs() { return length; } template <class ItemType> void UnsortedType<ItemType>::ResetList() { currentPos = -1; } template <class ItemType> void UnsortedType<ItemType>::GetNextItem(ItemType& item) { currentPos++; item = info [currentPos] ; } O(1) O(1) O(1) O(1) O(1) O(1)
  • 19. sortedtype.cpp #include "sortedtype.h" template <class ItemType> SortedType<ItemType>::SortedType() { length = 0; currentPos = -1; } template <class ItemType> void SortedType<ItemType>::MakeEmpty() { length = 0; } template <class ItemType> bool SortedType<ItemType>::IsFull() { return (length == MAX_ITEMS); } template <class ItemType> int SortedType<ItemType>::LengthIs() { return length; } template <class ItemType> void SortedType<ItemType>::ResetList() { currentPos = -1; } template <class ItemType> void SortedType<ItemType>::GetNextItem(ItemType& item) { currentPos++; item = info [currentPos]; }
  • 20. sortedtype.cpp #include "sortedtype.h" template <class ItemType> SortedType<ItemType>::SortedType() { length = 0; currentPos = -1; } template <class ItemType> void SortedType<ItemType>::MakeEmpty() { length = 0; } template <class ItemType> bool SortedType<ItemType>::IsFull() { return (length == MAX_ITEMS); } template <class ItemType> int SortedType<ItemType>::LengthIs() { return length; } template <class ItemType> void SortedType<ItemType>::ResetList() { currentPos = -1; } template <class ItemType> void SortedType<ItemType>::GetNextItem(ItemType& item) { currentPos++; item = info [currentPos]; } O(1) O(1) O(1) O(1) O(1) O(1)
  • 21. Inserting an Item into Unsorted List [0] 6 [1] 3 [2] 4 [3] 1 [4] 2 . . . . [MAX_ITEMS - 1] [0] 6 [1] 3 [2] 4 [3] 1 [4] 2 [5] 5 . . . [MAX_ITEMS - 1] Logical garbage Logical garbage length = 5 length = 6 Insert 5
  • 22. unsortedtype.cpp template <class ItemType> void UnsortedType<ItemType>::InsertItem(ItemType item) { info[length] = item; length++; }
  • 23. unsortedtype.cpp O(1) template <class ItemType> void UnsortedType<ItemType>::InsertItem(ItemType item) { info[length] = item; length++; }
  • 24. Inserting an Item into Sorted List [0] 1 [1] 2 [2] 4 [3] 6 [4] 8 . . . . [MAX_ITEMS - 1] [0] 1 [1] 2 [2] 4 [3] 5 [4] 6 [5] 8 . . . [MAX_ITEMS - 1] Logical garbage Logical garbage length = 5 length = 6 Insert 5
  • 25. 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++; }
  • 26. sortedtype.cpp template <class ItemType> void SortedType<ItemType>::InsertItem(ItemType item) { int location = 0; bool moreToSearch = (location < length); while (moreToSearch) { if(item > info[location]) { location++; moreToSearch = (location < length); } else if(item < info[location]) moreToSearch = false; } for (int index = length; index > location; index--) info[index] = info[index - 1]; info[location] = item; length++; } O(N) O(N) O(N)
  • 27. Deleting an Item from Unsorted List [0] 6 [1] 3 [2] 4 [3] 1 [4] 2 [5] 5 . . . [MAX_ITEMS - 1] [0] 6 [1] 3 [2] 4 [3] 5 [4] 2 . . . . [MAX_ITEMS - 1] Logical garbage Logical garbage length = 6 length = 5 Delete 1
  • 28. unsortedtype.cpp template <class ItemType> void UnsortedType<ItemType>::DeleteItem(ItemType item) { int location = 0; while (item != info[location]) location++; info[location] = info[length - 1]; length--; }
  • 29. unsortedtype.cpp template <class ItemType> void UnsortedType<ItemType>::DeleteItem(ItemType item) { int location = 0; while (item != info[location]) location++; info[location] = info[length - 1]; length--; } O(1) O(N) O(N)
  • 30. Deleting an Item from Sorted List [0] 1 [1] 2 [2] 4 [3] 5 [4] 6 [5] 8 . . . [MAX_ITEMS - 1] [0] 1 [1] 2 [2] 4 [3] 6 [4] 8 . . . . [MAX_ITEMS - 1] Logical garbage Logical garbage length = 6 length = 5 Delete 5
  • 31. sortedtype.cpp template <class ItemType> void SortedType<ItemType>::DeleteItem(ItemType item) { int location = 0; while (item != info[location]) location++; for (int index = location + 1; index < length; index++) info[index - 1] = info[index]; length--; }
  • 32. sortedtype.cpp template <class ItemType> void SortedType<ItemType>::DeleteItem(ItemType item) { int location = 0; while (item != info[location]) location++; for (int index = location + 1; index < length; index++) info[index - 1] = info[index]; length--; } O(N) O(N) O(N)
  • 33. Retrieving an Item from Unsorted List • Visit each element in the list, one by one, until the item is found.
  • 34. unsortedtype.cpp template <class ItemType> void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool &found) { int location = 0; bool moreToSearch = (location < length); found = false; while (moreToSearch && !found) { if(item == info[location]) { found = true; item = info[location]; } else { location++; moreToSearch = (location < length); } } }
  • 35. unsortedtype.cpp template <class ItemType> void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool &found) { int location = 0; bool moreToSearch = (location < length); found = false; while (moreToSearch && !found) { if(item == info[location]) { found = true; item = info[location]; } else { location++; moreToSearch = (location < length); } } } O(N)
  • 36. Retrieving an Item from Sorted List • Find 84 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
  • 37. Retrieving an Item from Sorted List • Find 84 • 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
  • 38. Retrieving an Item from Sorted List • Find 84 • 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
  • 39. Retrieving an Item from Sorted List • Find 84 • 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
  • 40. Retrieving an Item from Sorted List • Find 84 • 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
  • 41. Retrieving an Item from Sorted List • Find 84 • 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
  • 42. Retrieving an Item from Sorted List • Find 84 • 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
  • 43. Retrieving an Item from Sorted List • Find 84 • 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
  • 44. Retrieving an Item from Sorted List • Find 84 • 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
  • 45. Retrieving an Item from Sorted List • Find 84 • 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
  • 46. Retrieving an Item from Sorted List • Find 84 • 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
  • 47. Retrieving an Item from Sorted List • Find 84 • Step 4 • 84 found at the midpoint 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
  • 48. Retrieving an Item from Sorted List • Find 73 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
  • 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?
  • 63. template <class ItemType> void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) { int midPoint, first = 0, last = length - 1; bool moreToSearch = (first <= last); found = false; while (moreToSearch && !found) { midPoint = (first + last) / 2; if(item < info[midPoint]) { last = midPoint - 1; moreToSearch = (first <= last); } else if(item > info[midPoint]) { first = midPoint + 1; moreToSearch = (first <= last); } else { found = true; item = info[midPoint]; } } } sortedtype.cpp
  • 64. sortedtype.cpp O(logN) template <class ItemType> void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) { int midPoint, first = 0, last = length - 1; bool moreToSearch = (first <= last); found = false; while (moreToSearch && !found) { midPoint = (first + last) / 2; if(item < info[midPoint]) { last = midPoint - 1; moreToSearch = (first <= last); } else if(item > info[midPoint]) { first = midPoint + 1; moreToSearch = (first <= last); } else { found = true; item = info[midPoint]; } } }