390 likes | 401 Views
Sorted Lists. CS 308 - Data Structures. Sorted List Specification (partial). InsertItem (ItemType item) Function : Adds item to list
E N D
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 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> 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--; }
Linear Search Algorithm (cont.) (more efficient when the item we are searching for is not in the list)
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.
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 !!
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_goldfish Cost cost_of_elephants (approximation) • The low order terms in a function are relatively insignificant for largen n4 + 100n2 + 10n + 50 n4 • Or n4 + 100n2 + 10n + 50 and n4 have the same rate 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)
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)
Examples 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 (cont.’d) O(N) if (i<j) for ( i=0; i<N; i++ ) X = X+i; else X=0; Max ( O(N), O(1) ) = O (N) O(1)
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 case O(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 Algorithm 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; 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--; }
Exercises • 6, 8, 10, 13