0% found this document useful (0 votes)
15 views

08 SortedLists

Uploaded by

shajlala
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

08 SortedLists

Uploaded by

shajlala
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Sorted Lists

CS 302 - Data Structures


Sections 4.1 & 4.2
Sorted List Implementation
template<class ItemType>
class SortedType {
public:
void MakeEmpty();
bool IsFull() const;
int LengthIs() const;
void RetrieveItem(ItemType&, bool&);
void InsertItem(ItemType);
void DeleteItem(ItemType);
void ResetList();
bool IsLastItem();
void GetNextItem(ItemType&);
private:
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
InsertItem - Specification

InsertItem (ItemType item)


Function: Adds item to list
Preconditions: (1) List has been initialized,
(2) List is not full, (3) item is
not in list (4) List is sorted by key
member. Postconditions: (1) item is in
list, (2) List is still sorted.
Sorted List Implementation
template<class ItemType>
void SortedType<ItemType>::InsertItem(ItemType
item)
{
int location = 0;
bool found;
found = false;
while( (location < length) && !found) {
if (item < info[location])
found = true; O(N)
else
location++;
}
(cont)
Sorted List Implementation

for (int index = length; index > location; index--)


info[index] = info[index - 1]; O(N)

info[location] = item;
length++;
}
Total time: O(N)
DeleteItem - Specification
DeleteItem(ItemType item)
Function: Deletes the element whose key matches
item's key
Preconditions: (1) List has been initialized,
(2) Key member of item has been
initialized, (3) There is only one element
in list which has a key matching item's key, (4)
List is sorted by key member.
Postconditions: (1) No element in list has a key
matching item's key, (2) List is still sorted.
Sorted List Implementation
template<class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;

while (item != info[location]) O(N)


location++;

for (int index = location + 1; index < length; index++)


info[index - 1] = info[index];
length--;
O(N)
}
Total time: O(N)
RetrieveItem (ItemType& item,
Boolean& found)
• Function: Retrieves list element whose key matches
item's key (if present).

• Preconditions: (1) List has been initialized,


(2) Key member of item has been initialized.

• Postconditions: (1) 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, (2) List is unchanged.
Simplest idea: use Linear Search Algorithm
What if item is not in the list?

Can be implemented more efficiently when the


item we are searching for is not in the list!
Improving RetrieveItem()
template<class ItemType>
void SortedType<ItemType>::RetrieveItem (ItemType& item, bool&
found)
{
int location = 0;
found = false;
while ( (location < length) && !found) {
if ( item > info[location]) {
location++;
O(N)
else if(item < info[location])
location = length; // no reason to continue searching …
else {
found = true;
item = info[location];
}
}
Binary Search Algorithm
Split the current search area in half, and if the
item is not found there, then search the
appropriate half.
- Search for 24:
Binary Search Algorithm (cont.)
template<class ItemType>
void SortedType<ItemType>::
RetrieveItem(ItemType& item, bool& found)
{
int midPoint;
int first = 0;
int last = length - 1;
found = false;
while( (first <= last) && !found) {
midPoint = (first + last) / 2;
if (item < info[midPoint])
last = midPoint - 1; O(logN)
else if(item > info[midPoint])
first = midPoint + 1;
else {
found = true;
item = info[midPoint];
}
}
}
Is Binary Search more efficient?
(1) Number of iterations:
– e.g, for a list of 11 elements, it never executed more than
4 times (e.g., approximately log2 11 times)!!
– Linear Search, on the other hand, can execute up to 11
times !!
Number of Iterations
Length Linear Search Binary Search
(average)
10 5.5 3.3
100 50.5 6.6
1,000 500.5 10
10,000 5000.5 13.3
Is Binary Search more efficient?
(cont’d)
(2) Number of computations per iteration:
– Binary search does more work per iteration
than Linear Search
Linear search iterations Binary search iterations
while ( (location < length) && !found) { while( (first <= last) && !found) {
midPoint = (first + last) / 2;
if ( item > info[location]) { if (item < info[midPoint])
location++; last = midPoint - 1;
else if(item < info[location]) else if(item > info[midPoint])
location = length; // no reason to continue searching first = midPoint + 1;
… else {
else { found = true;
found = true; item = info[midPoint];
item = info[location]; }
}
}
Is Binary Search more efficient?
(cont’d)

• Overall, it can be shown that:


– If the number of components is small
(typically, under 20), then Linear Search is
faster.
– If the number of components is large, then
Binary Search is faster.
Summary of Results
Big-O Comparison of List Operations
Operation Unsorted Lists Sorted Lists
MakeEmpty O(1) O(1)
LengthIs O(1) O(1)
IsFull O(1) O(1)
ResetList O(1) O(1)
GetNextItem O(1) O(1)
RetrieveItem O(N) O(log N)
InsertItem O(1) O(N)
DeleteItem O(N) O(N)
Exercise 3 (page 185): Write a client
function that splits a sorted list into two
sorted lists using the following specification.

SplitLists (SortedType list, ItemType item,


SortedType& list1, SortedType& list 2)

Function: Divides list into two lists according to the key of


item.
Preconditions: list has been initialized and is not empty.
Postconditions: list1 contains all the items of list whose keys
are less than or equal to item’s key. list2 contains all the
items of list whose keys are greater than item’s key.
void SplitLists(const SortedType& list, ItemType item,
SortedType& list1, SortedType& list2)
{
ItemType listItem;

list1.MakeEmpty();
list2.MakeEmpty();
list.ResetList();
What is the running time
while (!list.IsLastItem()) { using big-O?
list.GetNextItem(listItem);
if(listItem > item)
O(N2)
list2.InsertItem(listItem);
else
list1.InsertItem(listItem);
}
}
Example
• Suppose we have a million elements in an sorted
list; which algorithm would be faster?

(1) A binary search on a 500-MHz computer or

(2) A linear search on a 5-GHz computer


Example (cont’d)
• Assumptions:
(1) Each instruction on the 5-GHz computer is 10 times
faster than each instruction on the 500-MHz computer.

(2) Each iteration of a linear search will be twice as fast as


each iteration of a binary search on the same computer.
Example (cont’d)

• Consider number of iterations first:


Binary Search Linear Search
log2(1,000,000) ~ 20 1,000,000 iterations (worst-case)
(worst-case) or 500,000 (average-case)

• Binary search will be 500,000/20 = 25,000


faster than linear search.
Example (cont’d)

• Assuming same computers – use assumption (1):


– Each iteration of linear search is twice as fast as an
iteration of binary search

– Binary search would be 25,000/2 = 12,500 faster!


Example (cont’d)

• Assuming different computers – use assumptions (1)


and (2):

– Each instruction of binary search on the 500-MHz


computer would be 10 times slower than each instruction
of linear search on the 5-GHz computer.

– Binary search will be 12,500/10 = 1250 times faster on the


500-MHz computer than linear search on the 5-GHz
computer!

You might also like