algorithm best worst time complexity and space code example
Example 1: algorithms and their time and space complexity
-----------------------------------------------------------------------------
|Sorting Algorithm | Best Case | Average Case | Worst Case |
|------------------|------------------|------------------|------------------|
|Selection Sort | Ω(n²) | θ(n²) | O(n²) |
|Bubble Sort | Ω(n) | θ(n²) | O(n²) |
|Insertion Sort | Ω(n) | θ(n²) | O(n²) |
|Merge Sort | Ω(n logn(n)) | θ(n logn(n)) | O(n logn(n)) |
|Quick Sort | Ω(n logn(n)) | θ(n logn(n)) | O(n²) |
|Heap Sort | Ω(n logn(n)) | θ(n logn(n)) | O(n logn(n)) |
|Radix Sort | Ω(nk) | θ(nk) | O(nk) |
|Bucket Sort | Ω(n + k) | θ(n + k) | O(n²) |
-----------------------------------------------------------------------------
Example 2: algorithms and their time and space complexity
--------------------------------
|Input Size | Max Complexity |
|-----------|------------------|
|10^18 | O(logn) |
|10^8 | O(n) |
|10^7 | O(nlogn) |
|10^4 | O(n^2) |
|10^2 | O(n^3) |
|9*10 | O(n^4) |
--------------------------------