4 - Complexity Analysis
4 - Complexity Analysis
4 - Complexity Analysis
Time Complexity:
Best Case: In the best case, the key might be present at the first index. So the best case complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index i.e., opposite to the end from which the
search has started in the list. So the worst-case complexity is O(N) where N is the size of the list.
Auxiliary Space: O(1) as except for the variable to iterate through the list, no other variable is used.
Unsorted Lists: When we have an unsorted array or list, Sequential/Linear search is most commonly used to find any
element in the collection.
Small Data Sets: Sequential/Linear Search is preferred over binary search when we have small data sets with
Simple Implementation: Sequential/Linear Search is much easier to understand and implement as compared to
Binary Search or Ternary Search.
Sequential/Linear search can be used irrespective of whether the array is sorted or not. It can be used on arrays of
any data type.
Sequential/Linear search has a time complexity of O(N), which in turn makes it slow for large datasets.
Time Complexity:
Auxiliary Space: O(1), If the recursive call stack is considered then the auxiliary space will be O(logN).
Binary search can be used as a building block for more complex algorithms used in machine learning, such as
algorithms for training neural networks or finding the optimal hyperparameters for a model.
It can be used for searching in computer graphics such as algorithms for ray tracing or texture mapping.
Binary search is faster than Sequential/Linear search, especially for large arrays.
More efficient than other searching algorithms with a similar time complexity, such as interpolation search or
exponential search.
Binary search is well-suited for searching large datasets that are stored in external memory, such as on a hard drive
or in the cloud.
Binary search requires that the data structure being searched be stored in contiguous memory locations.
Binary search requires that the elements of the array be comparable, meaning that they must be able to be ordered.
Complexity Analysis of Selection Sort
2
Time Complexity: The time complexity of Selection Sort is O(N ) as there are two nested loops:
Another loop to compare that element with every other Array element = O(N)
2
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N )
Auxiliary Space: O(1) as the only extra memory used is for temporary variables while swapping two values in Array. The
selection sort never makes more than O(N) swaps and can be useful when memory writing is costly.
2
Selection sort has a time complexity of O(N ) in the worst and average case.
Does not preserve the relative order of items with equal keys which mean it is not stable.
Complexity Analysis of Bubble Sort :
2
Time Complexity: O(N )
Auxiliary Space: O(1)
It is a stable sorting algorithm, meaning that elements with the same key value maintain their relative order in the
sorted output.
Bubble sort has a time complexity of O(N2) which makes it very slow for large data sets.
Bubble sort is a comparison-based sorting algorithm, which means that it requires a comparison operator to determine
the relative order of elements in the input data set. It can limit the efficiency of the algorithm in certain cases.