Data structures and Algorithms Flashcards
What is time complexity analyzed for?
1) Very large input size
2) Worst case scenario
What is the time complexity of T(n) = 2n^2+3n+1?
1) Drop lower order terms
2) Drop constants
Time complexity is O(N) = n^2
How do you calculate the time complexity of a single loop? for (int i = 1; i <= n; i++) { x = y + z; }
for (int i = 1; i <= n; i++) { x = y + z; //constant time - C } Time complexity is C*N. Since you traverse N number of elements.
How do you calculate the time complexity of a nested loop?
1) Figure out how many times each loop will pass
2) Multiply each loop
How do you calculate time complexity of sequential statements?
You add the time complexities of each statement.
How do you calculate time complexity of if else statements?
You take the worse complexity of the two conditions.
What is the order of time complexities
O(logN), O(NlogN), O(n!), O(1), O(n), O(c^n), O(n^c). From worst to best.
O(n!) > O(c^n) > O(n^c) > O(NLogN) > O(n) > O(logN) > O(1)
What is the time complexity of selection sort?
Best: n^2
Average: n^2
Worst: n^2
What is the time complexity of bubble sort?
Best: `n
Avergage: n^2
Worst: n^2
What is the time complexity of insertion sort?
Best: n
Average: n^2
Worst: n^2
What is the time complexity of heap sort?
All: NLogN
What is the time complexity of quick sort?
Best, Average: NlogN
Worst: N^2
What is the time complexities of merge sort?
All: NlogN
What is the time complexities of bucket sort?
Best, Average: N+K
Worst: N^2
What is the time complexities of radix sort?
Best, Average, Worst: N*K