0% found this document useful (0 votes)
14 views18 pages

Module 01 Big-O and DS in Practice

Uploaded by

bekamdawit551
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

Module 01 Big-O and DS in Practice

Uploaded by

bekamdawit551
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Big-O in Practice

Data Structure Examples and


Applications
Give the most appropriate big-O time
complexity for the following algorithms
void print(int array[], int size) {
cout << "{"; print
for(int i = 0; i < size; i++){ best-case:
cout << array[i] << ", ";
}
cout << "}" << endl; worst-case:
}

int getMax(int array[], int size) {


if(size <= 0) {return -1;}
getMax
int max = array[0];
best-case:
for(int i = 0; i < size;i++) {
if(max < array[i]){
max = array[i];
worst-case:
}
}
return max;
}
Give the most appropriate big-O time
complexity for the following algorithms
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)
}

int getMax(int array[], int size) {


if(size <= 0) {return -1;}
getMax
int max = array[0];
best-case: O(n)
for(int i = 0; i < size;i++) {
if(max < array[i]){
max = array[i];
worst-case: O(n)
}
}
return max;
}
Give the most appropriate big-O time
complexity for the following algorithms
int getMaxIndex(int array[], int size) {
if(size <= 0) {return -1;} getMaxIndex
int maxIndex = 0; best-case:
for(int i = 0; i < size;i++) {
if(array[maxIndex] < array[i]){
maxIndex = i;
} worst-case:
}
return maxIndex;
}

int getMaxIndexFromStart(int array[], int size, int startIndex) {


if(size <= 0 || startIndex >= size) {return -1;} getMaxIndexFromStart
int maxIndex = startIndex; best-case:
for(int i = startIndex; i < size;i++) {
if(array[maxIndex] < array[i]){
maxIndex = i;
} worst-case:
}
return maxIndex;
}
Give the most appropriate big-O time
complexity for the following algorithms
int getMaxIndex(int array[], int size) {
if(size <= 0) {return -1;} getMaxIndex
int maxIndex = 0; best-case: O(n)
for(int i = 0; i < size;i++) {
if(array[maxIndex] < array[i]){
maxIndex = i;
} worst-case: O(n)
}
return maxIndex;
}

int getMaxIndexFromStart(int array[], int size, int startIndex) {


if(size <= 0 || startIndex >= size) {return -1;} getMaxIndexFromStart
int maxIndex = startIndex; best-case: O(1) assuming we start
for(int i = startIndex; i < size;i++) {
if(array[maxIndex] < array[i]){
at the very end.
maxIndex = i;
}
}
worst-case: O(n)
return maxIndex;
}
Give the most appropriate big-O time
complexity for the following algorithms
int findKeyIndex(int array[], int size, int searchKey) {
if(size <= 0) {return -1;} findKeyIndex
for(int i = 0; i < size;i++) { best-case:
if(array[i] == searchKey){
return i;
}
} worst-case:
return -1;
}

bool contains(int array[], int size, int searchKey) { contains


if(findKeyIndex(array, size, searchKey) != -1){ best-case:
return true;
}
return false;
} worst-case:
Give the most appropriate big-O time
complexity for the following algorithms
int findKeyIndex(int array[], int size, int searchKey) {
if(size <= 0) {return -1;} findKeyIndex
for(int i = 0; i < size;i++) { best-case: O(1)
if(array[i] == searchKey){
return i;
}
} worst-case: O(n)
return -1;
}

bool contains(int array[], int size, int searchKey) { contains


if(findKeyIndex(array, size, searchKey) != -1){ best-case: O(1)
return true;
}
return false;
} worst-case: O(n)

It calls findKeyIndex and adds a


fixed / constant amount of work.
Suppose we assume the array is sorted.
void print(int array[], int size) {
cout << "{"; print
for(int i = 0; i < size; i++){ best-case:
cout << array[i] << ", ";
}
cout << "}" << endl; worst-case:
}

int getMax(int array[], int size) {


// array is sorted, smallest to largest.
getMax
best-case:
}

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

int getMax(int array[], int size) {


// array is sorted, smallest to largest.
getMax
return array[size-1];
best-case: O(1)
}

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:

bool contains(int array[], int size, int searchKey) { contains


if(findKeyIndex(array, size, searchKey) != -1){ best-case:
return true;
}
return false;
} worst-case:
Suppose we assume the array is sorted.
int findKeyIndex(int array[], int size, int searchKey)
{ findKeyIndex
// array is sorted, smallest to largest. best-case: O(1)
// … use binary search
}
worst-case: O(log n)

bool contains(int array[], int size, int searchKey) { contains


if(findKeyIndex(array, size, searchKey) != -1){ best-case: O(1)
return true;
}
return false;
} worst-case: O(log n)
It calls findKeyIndex and adds a
fixed / constant amount of work
Variations on Set
Typical variation:

Insert new values at size position


Use linear search to check for duplicates

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

Variations on Set need to call contains.

Typical variation: We may need to call contains to


determine if an element is in the
Insert new values at size position set.
Use linear search to check for duplicates
We may need to shift all elements
size when removing the first element.

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 O(1) O(n)

remove O(n) O(n)

contains O(1) O(n)


Variations on Set: Sorted Sets
Sorted variation:

Insert new values at in their sorted position


Use binary search to check for duplicates

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).

Best case for remove means we


6 12 19 discover the element Is not in the
array.
0 1 2 3 4 5 6 7
operations Best-case Big-O Worst-case Big-O abstract view

add O(1) O(n)


{6, 12, 19}
remove O(log n) O(n)

contains O(1) O(log n)


Explaining each case for a sorted array Set
• add
• element already exists in the set.
• Immeidate discover the element at array[mid].
• O(1), best case
• Dicosver the element is in the set after a full binary search.
• O(log n).
• For any of these, there is no shift as the element in the set (should not be added again).
• element does not exist in the set.
• Discover the element is not in the set and its position should be near index 0.
• O(log n) for the binary search + O(n) for the shifts. Leads to O(n) worst case
• Discover the element is not in the set and its position sould be near index "size".
• O(log n) for the binary search + O(1) for inserting at position "size". O(log n).
Explaining each case for a sorted array Set
• remove
• element exists in the set.
• Immeidate discover the element at array[mid], remove it and shift elements.
• O(1) to to discover it in the middle + O(n) for the shifts. Leads to O(n)
• Element is in the middle, have to do n/2 shifts (1/2)n = c n --> O(n)
• Dicosver the element in the set at position 0, remover it and shift.
• O(log n) to discover the element + O(n) for the shifts. Leads to O(n) worst case.
• These have different actual runtimes, but their big-O is the same.
• (log n + n) is O(n) and (c + n) is O(n).
• element does not exist in the set.
• Discover the element is not in the set.
• O(log n) for the binary search leads to O(log n). best case.
• No way to discover the element is not in the set in less than O(log n) steps.
Side by Side: comparing two Set variations
Time cost in practice
Consider these average case operations when the element needs to be added or removed.

operation searching / check duplicate adding to the array


With an unsorted array
will need to shift
add n + C elements to put in
sorted order
With a sorted array
add log n + n

operation searchering for element removing / reordering


with an unsorted array Both variations
will need to find
remove n + n and shift values.
With a sorted array
remove log n + n

You might also like