Examples of Algorithms which has O(1), O(n log n) and O(log n) complexities

If you want examples of Algorithms/Group of Statements with Time complexity as given in the question, here is a small list – O(1) time Accessing Array Index (int a = ARR[5];) Inserting a node in Linked List Pushing and Poping on Stack Insertion and Removal from Queue Finding out the parent or left/right child of … Read more

What’s the difference between backtracking and depth first search?

Backtracking is a more general purpose algorithm. Depth-First search is a specific form of backtracking related to searching tree structures. From Wikipedia: One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking. It uses backtracking as part of its … Read more

How to create the most compact mapping n → isprime(n) up to a limit N?

The fastest algorithm for general prime testing is AKS. The Wikipedia article describes it at lengths and links to the original paper. If you want to find big numbers, look into primes that have special forms like Mersenne primes. The algorithm I usually implement (easy to understand and code) is as follows (in Python): def … Read more

Red black tree over avl tree

What’s the main reason for choosing Red black trees instead of AVL trees? Both red-black trees and AVL trees are the most commonly used balanced binary search trees and they support insertion, deletion and look-up in guaranteed O(logN) time. However, there are following points of comparison between the two: AVL trees are more rigidly balanced … Read more

How Do I Choose Between a Hash Table and a Trie (Prefix Tree)?

Advantages of tries: The basics: Predictable O(k) lookup time where k is the size of the key Lookup can take less than k time if it’s not there Supports ordered traversal No need for a hash function Deletion is straightforward New operations: You can quickly look up prefixes of keys, enumerate all entries with a … Read more