Module 01 Big-O and DS in Practice
Module 01 Big-O and DS in Practice
worst-case:
Suppose we assume the array is sorted.
void print(int array[], int size) {
cout << "{"; print
for(int i = 0; i < size; i++){ best-case: O(n)
cout << array[i] << ", ";
}
cout << "}" << endl; worst-case: O(n)
}
worst-case: O(1)
Suppose we assume the array is sorted.
int findKeyIndex(int array[], int size, int searchKey)
{ findKeyIndex
// array is sorted, smallest to largest. best-case:
// …
}
worst-case:
size
abstract view
6 19 12
{6, 19, 12}
0 1 2 3 4 5 6 7
operations Best-case Big-O Worst-case Big-O
add
remove
contains
Remember: add and remove both
abstract view
6 19 12
{6, 19, 12}
0 1 2 3 4 5 6 7
operations Best-case Big-O Worst-case Big-O
size
abstract view
6 12 19
{6, 12, 19}
0 1 2 3 4 5 6 7
operations Best-case Big-O Worst-case Big-O
add
remove
contains
Remember: add and remove both
need to call contains.
Variations on Set: Sorted Sets Best case for add we find the
Sorted variation: element in the middle
(immediately discover a duplicate).
Insert new values at in their sorted position
Use binary search to check for duplicates Inserting or removing in a sorted
position means shifting elements
size over leading to O(n).