Algorithms Flashcards
(2 cards)
1
Q
Difference between Depth first search (DFS) and Breadth first search (BFS) in graph traverse?
A
DFS - explores as far along each branch as possible before backtracking. It uses a stack (either explicitly or through recursion) and is memory efficient for deep graphs.
BFS - explores all neighbours at the present depth before moving on to nodes at the next depth level. It uses a queue and is memory efficient for wide graphs
2
Q
What are some common sorting algorithms and their time complexites
A
- Quick sort - average time complexity is O(n log n), worst case is O(n^2)
- Merge sort - time complexity is O(n log n) for all cases
- Bubble sort - time complexity is O(n^2) for average and worst cases
- Insertion sort - AV and worst case time complexity is O(n^2)