DeleteItem: Finding the item location: 1 Key Bradley has been matched length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]
DeleteItem: after the operation location: 1 length 3 info [ 0 ] Maxwell [ 1 ] Hsing [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] * Copy last list element to where key “Bradley” was before * length has been decremented
void UnsortedType::DeleteItem ( ItemType item )
// Pre: item’s key has been initialized.
// An element in the list has a key that matches item’s.
// Post: No element in the list has a key that matches item’s.
int location = 0 ;
while (item.key != info[location].key)
// move last element into position where item was located
info [location] = info [length - 1 ] ;
// Pre: N/A
// Post: the list is empty
length = 0;
We do not have to do anything to the array that holds the list items to make a list empty.
void UnsortedType::ResetList ( )
// Pre: List has been initialized.
// Post: Current position is prior to first element in list.
currentPos = -1 ;
void UnsortedType::GetNextItem ( ItemType& item )
// Pre: List has been initialized. Current position is defined.
// Element at current position is not last in list.
// Post: Current position is updated to next position.
// item is a copy of element at current position.
item = info [currentPos] ;
How to use the List implemented?
CreateFromFile(ifstream &, UnsortedType&)
Users do not need to know how the list is implemented.
// Pre: list has been initialized.
// Post: Each component in list has been written.
void PrintList(UnsortedType & list)
length = list.GetLength( );
for (int counter = 1; counter <= length; counter++)
Order of Magnitude of a Function
The order of magnitude, or Big-O notation, of a function expresses the computing time of a problem as the term in a function that increases most rapidly relative to the size of a problem .
수행시간의 Big-O 표기 : 수행시간에 대한 함수 표기방법으로 문제의 크기와 관련하여 수행시간을 가장 빠르게 증가시키는 함수의 항으로 나타냄
O : 함수를 ( 증가속도에 따라 ) 분류하는 방법
Names of Orders of Magnitude
O(1) bounded (by a constant) time // 상수시간
O(log 2 N) logarithmic time
O(N) linear time
O(N*log 2 N) N*log 2 N time
O(N 2 ) quadratic time
O( 2 N ) exponential time
The Story of Big O
Algorithm running time (work) as a function of N (size of the input)
F(N)=N 4 +100N 2 +10N+50
Order of magnitude of F(N), Big-O notation
an approximation of F(N) with the term in the function that increases fastest with N
For above F(N), which part increases fastest when N increases ?
N 4 , for large N, N 4 is much larger than 100N 2 , 10N
F(N) is of order N 4 , or is O(N 4 )
Algorithm Growth Rates Figure Time requirements as a function of the problem size n
Algorithm A requires time proportional to n 2
Algorithm B requires time proportional to n
A is more time efficient than B
Big-O Comparison of List Operations
OPERATION UnsortedList 의 배열구현 RetrieveItem O(N) InsertItem Find O(1) Put O(1) Combined O(1) DeleteItem Find O(N) Put O(1) swap Combined O(N)