DEV Community

Rajesh Mishra
Rajesh Mishra

Posted on • Updated on

Algorithm Code Complexity

Code Complexity compute it by using the control flow graph of the program. Cyclomatic complexity measures the number of nested conditions within the code, such as those created by for, if/else, switch, while, and until. The greater the number of conditions (as in example b from above), the greater the complexity.

 

Time complexity:
 Worst Case Scenario Average Case Scenario Best Case Scenario
Accessing an element O(1) O(1) O(1)
Updating an element O(1) O(1) O(1)
Deleting an element O(n) O(n) O(1)
Inserting an element O(n) O(n) O(1)
Searching for an element O(n) O(n) O(1)

  Algorithm complexity:  
Worst Case Average Case Best Case Space Complexity
Quicksort O(n2) O(n log(n)) O(n log(n)) O(log(n))
Mergesort O(n log(n)) O(n log(n)) O(n log(n)) O(n)
Heapsort O(n log(n)) O(n log(n)) O(n log(n)) O(1)
Bubble Sort O(n2) O(n2) O(n) O(1)
Insertion Sort O(n2) O(n2) O(n) O(1)
Selection Sort O(n2) O(n2) O(n2) O(1)

Source: Java Algos


Top comments (0)