See implementation in C++, Java, Python, Typescript
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform.
def do_random_stuff():
foo = True # 1 operation
bar = 8 * 3 # 1 operation
if bar < 20: # 1 operation
print("bar is small") # 1 operation
for i in range(0, bar): # 24 operations
print(i) # 1 operation
do_random_stuff() # O(1)
do_random_stuff() # O(1)
do_random_stuff() # O(1)
Run time for doRandomStuff
is
#include <iostream>
#include <vector>
using namespace std;
bool isContains(int target, vector<int> numbers){
for(int number: numbers){
if(target == number){
return true;
}
}
return false;
}
int main() {
cout << isContains(8, {1, 2, 3, 4, 5, 8, 10}) << endl; // 1
// O(n)
cout << isContains(8, {1, 2, 3, 4, 5, 6, 7, 9, 10}) << endl; // 0
// O(n)
cout << isContains(8, {1, 2, 100, 200, 300, 500}) << endl; // 0
// O(n)
}
Run time for isContains
function is
#include <iostream>
#include <vector>
using namespace std;
void printPairs(int n){
for(int i=1; i<=n; i++){
for(int j=i+1; j<=n; j++){
cout << "(" << i << ", " << j << ") ";
}
cout << endl;
}
}
int main() {
printPairs(4);
}
Output
(1, 2) (1, 3) (1, 4)
(2, 3) (2, 4)
(3, 4)
Run time for printPairs
is
// binaray search - nums is sorted array
#include <iostream>
#include <vector>
using namespace std;
bool isContains(int target, vector<int> numbers){
int lo = 0; // 1 operation
int hi = numbers.size(); // 1 operation
int mid;
while(lo <= hi){ // log(n) times
mid = (lo + hi) / 2;
if(numbers[mid] == target){ // 1 operation
return true;
}
else if(target > numbers[mid]){
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return false;
}
int main() {
cout << isContains(8, {1, 2, 3, 4, 5, 8, 10}) << endl; // true
// O(log(n))
cout << isContains(8, {1, 2, 3, 4, 5, 9, 10}) << endl; // false
// O(log(n))
cout << isContains(8, {4, 5, 6, 7, 8}) << endl; // true
// O(log(n))
cout << isContains(8, {8, 9, 10, 11, 12}) << endl; // true
// O(log(n))
}
Run time for isContains
is
void combine = (vector<int> nums) => {
foo(nums); // O(1)
fuu(nums); // O(log(n))
bar(nums); // O(n)
baz(nums); // O(n^2)
}
combine({1, 2, 3, 4, 5, 6}); // O(n^2)
Runtime for combine()
is
- bigocheatsheet.com
- Time complexity, wikipedia
- Space complexity, wikipedia
▶️ Asymptotic Notation, CS50▶️ 2020, Data Structures in Typescript #3 - Big-O Algorithm Analysis, Jeff Zhang