Download presentation
Presentation is loading. Please wait.
1
Sorted Lists CS 308 - Data Structures
2
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.
3
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.
4
Sorted List Implementation template 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; };
7
Sorted List Implementation (cont.) template void SortedType ::InsertItem(ItemType item) { int location = 0; bool found; found = false; while( (location < length) && !found) { if (item < info[location]) found = true; else location++; } (continues)
8
Sorted List Implementation (cont.) for (int index = length; index > location; index--) info[index] = info[index - 1]; info[location] = item; length++; }
10
Sorted List Implementation (cont.) template void SortedType ::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--; }
11
(more efficient when the item we are searching for is not in the list) Linear Search Algorithm (cont.)
12
Improving RetrieveItem() Linear Search Algorithm: stop when you pass the place where the item would be if it were there. template void SortedType ::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]; }
13
Binary Search Algorithm Split the current search area in half, and if the item is not found there, then search the appropriate half.
15
- Search for 24:
16
Binary Search Algorithm (cont.) template void SortedType :: 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]; }
17
Is Binary Search more efficient? Number of iterations: –For a list of 11 elements, it never executed more than 4 times (e.g., approximately log 2 11 times)!! –Linear Search, on the other hand, can execute up to 11 times !! Average Number of Iterations LengthLinear SearchBinary Search 105.52.9 10050.55.8 1,000500.59.0 10,0005000.512.0
18
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.
19
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.)
20
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.
21
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;
22
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 large n n 4 + 100n 2 + 10n + 50 n 4 same rate of growth Or n 4 + 100n 2 + 10n + 50 and n 4 have the same rate of growth
23
Big-O notation In mathematics, we approximate functions using Big-O notation We say that n 4 + 100n 2 + 10n + 50 is of the order of n 4 or O(n 4 ) We say that 10n 3 + 2n 2 is O(n 3 ) We say that n 3 - n 2 is O(n 3 ) We say that 10 is O(1), We say that 1273 is O(1)
24
Big-O notation (cont.’d) g(N) <= cf(N) O(f(N)) is the set of all functions g(N) such that g(N) <= cf(N)
25
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
26
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(N 2 )
27
Common orders of magnitude
29
Running time of various statements
30
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(N 2 ) Examples
31
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)
32
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
33
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
34
Linear Search Algorithm template void SortedType ::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)
35
Binary Search Algorithm template void SortedType ::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]; }
36
- InsertItem takes O(length) time template void SortedType ::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++; }
37
- DeleteItem takes O(length) time template void SortedType ::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--; }
38
Summary of Results Big-O Comparison of List Operations OperationUnsorted ListsSorted Lists MakeEmptyO(1) LengthIsO(1) IsFullO(1) ResetListO(1) GetNextItemO(1) RetrieveItemO(N)O(N) or O(log N) InsertItemO(1)O(N) DeleteItemO(N)
39
Exercises 6, 8, 10, 13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.