sorted lists cs 308 - data structures. sorted list specification (partial) insertitem (itemtype...

39
Sorted Lists CS 308 - Data Structures

Post on 19-Dec-2015

233 views

Category:

Documents


2 download

TRANSCRIPT

Sorted Lists

CS 308 - Data Structures

Sorted List Specification (partial)

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 Specification (partial)DeleteItem(ItemType item)Function: Deletes the element whose key matches

item's keyPreconditions: (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>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;};

Sorted List Implementation (cont.)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; else location++;}

(continues)

Sorted List Implementation (cont.)

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

info[index] = info[index - 1];

info[location] = item;

length++;

}

Sorted List Implementation (cont.)

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--;}

(more efficient when the item we are searching for is not in the list)

Linear Search Algorithm (cont.)

Improving RetrieveItem() Linear Search Algorithm: stop when you pass the place

where the item would be if it were there.

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++; else if(item < info[location]) location = length; // no reason to continue 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; else if(item > info[midPoint]) first = midPoint + 1; else { found = true; item = info[midPoint]; } }}

Is Binary Search more efficient? • Number of iterations:

– 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 !!

Average Number of Iterations

Length Linear Search Binary Search

10 5.5 2.9

100 50.5 5.8

1,000 500.5 9.0

10,000 5000.5 12.0

Is Binary Search more efficient? (cont.)

• Number of computations: – Binary search does more computations than

Linear Search

• Overall:– If the number of components is small (say,

under 20), then Linear Search is faster.– If the number of components is large, then

Binary Search is faster.

Analysis of Algorithms

What is the goal?

• To compare algorithms!!

• How running time increases as the size of the problem increases.

• Other factors can also be considered (e.g., memory requirements, programmer's effort etc.)

How do we analyze algorithms?

• We need to define a number of objective measures.(1) Compare execution times.

Problem: times are specific to a particular computer !!

(2) Count the number of statements executed.

Problem: number of statements vary with the programming language as well as the style of the

individual programmer.

How do we analyze algorithms?(cont.)

Algorithm 1 Algorithm 2

 

arr[0] = 0; for(i=0; i<N; i++)

arr[1] = 0; arr[i] = 0;

arr[2] = 0;

...

arr[N-1] = 0;

Order of magnitude

• Consider the example of buying elephants and goldfish

Cost: cost_of_elephants + cost_of_goldfishCost cost_of_elephants (approximation)

• The low order terms in a function are relatively insignificant for large n

n4 + 100n2 + 10n + 50 n4

• Or n4 + 100n2 + 10n + 50 and n4 have the same same rate of growthrate of growth

Big-O notation

• In mathematics, we approximate functions using Big-O notation

• We say that n4 + 100n2 + 10n + 50 is of the order of n4 or O(n4)

• We say that 10n3 + 2n2 is O(n3)

• We say that n3 - n2 is O(n3)

• We say that 10 is O(1),

• We say that 1273 is O(1)

Big-O notation (cont.’d)

• O(f(N)) is the set of all functions g(N) such that g(N) <= cf(N)g(N) <= cf(N)

Computing running time• Associate a "cost" with each statement and find the

"total cost“ by finding the total number of times each statement is executed.

• Express running time in terms of the size of the problem. Algorithm 1 Algorithm 2

Cost Cost arr[0] = 0; c1 for(i=0; i<N; i++) c2 arr[1] = 0; c1 arr[i] = 0; c1 arr[2] = 0; c1 ... arr[N-1] = 0; c1  ----------- ------------- c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 = (c2 + c1) x N + c2

Computing running time (cont.)

• Both algorithms are of the same order: O(N)

Cost

  sum = 0; c1

for(i=0; i<N; i++) c2

for(j=0; j<N; j++) c2

sum += arr[i][j]; c3

------------

c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N = O(N2)

Common orders of magnitude

Running time of various statements

i = 0;

while (i<N) {

X=X+Y; // O(1)

result = mystery(X); // O(N), just an example...

i++; // O(1)

}

• The body of the while loop: O(N)

• Loop is executed: N times

N x O(N) = O(N2)

Examples

if (i<j)

for ( i=0; i<N; i++ )

X = X+i;

else

X=0;

Max ( O(N), O(1) ) = O (N)

O(N)

O(1)

Examples (cont.’d)

Analysis of the Unsorted List Implementation

• MakeEmpty, LengthIs, and IsFull take O(1) time.• ResetList and GetNextItem take also O(1) time.• RetrieveItem takes O(length) time (worst case)

O(1) time in the best case O(length/2)time in the average caseO(length) time in the worst case

• InsertItem takes O(1) time• DeleteItem takes O(length) time (worst case)

Find the item first: O(length) time (worst case)Delete the item: O(1) time

Analysis of the Sorted List Implementation

• MakeEmpty, LengthIs, and IsFull take O(1) time.

• ResetList and GetNextItem take also O(1) time.

• RetrieveItem takes O(length) time

Linear Search Algorithm

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++; else if(item < info[location]) location = length; // no reason to continue   else { found = true; item = info[location]; }}

O(length)

Binary Search Algorithmtemplate<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; else if(item > info[midPoint]) first = midPoint + 1; O(log(length)) else { found = true; item = info[midPoint]; } }}

- InsertItem takes O(length) time 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; else location++;}for (int index = length; index > location; index--) info[index] = info[index - 1]; info[location] = item; length++;}

- DeleteItem takes O(length) time

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--;}

Summary of ResultsBig-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(N) or O(log N)

InsertItem O(1) O(N)

DeleteItem O(N) O(N)

Exercises

• 6, 8, 10, 13