You are on page 1of 2

SORTING ALGORITHMS AND THEIR COMPLEXITY

Types Time Complexity Explanation of Time Complexity


Bubble sort O(n2) The time complexity of bubble sort is O(n2),
where n is the number of elements in the
input list. This is because the algorithm
needs to iterate over the input list at least
twice: once for each pass through the list,
and once for each comparison within each
pass. However, in the best case the
complexity is O(n) if list is already sorted.
Insertion sort O(n2) The time complexity of the insertion sort
algorithm is O(n2), where n is the number
of elements in the input list. This is because
the algorithm needs to iterate over the
input list at least twice: once to find the
correct position for each element, and once
to insert the element into its correct
position. However, in the best case the
complexity is O(n) if list is already sorted.
Merge sort O(nlogn) The time complexity of a merge sort
algorithm is O(nlogn), where n is the size of
the input. This means that the number of
steps the algorithm takes increases
logarithmically with the size of the input.
Quick sort Average Case: O(nlogn) The time complexity of a quick sort
Worst Case: O(n2) algorithm is O(nlogn) on average, but it can
degrade to O(n2) in the worst case. This
means that the number of steps the
algorithm takes is usually logarithmic with
the size of the input, but it can become
quadratic in the worst case.

TIME COMPLEXITIES OF SAMPLE PROBLEMS

Problems Time Complexity


An algorithm that processes a list of n elements The time complexity of this algorithm is O(n),
and performs a constant number of operations since the number of operations performed is
on each element. directly proportional to the size of the input (n
elements).
An algorithm that processes a list of n elements The time complexity of this algorithm is O(n3),
and performs O(n2) operations on each since the number of operations performed is
element. O(n2) on each element, and there are n
elements in the input list.
An algorithm that processes a list of n elements The time complexity of this algorithm is O(n!n),
and performs O(n!) operations on each since the number of operations performed is
element. O(n!) on each element, and there are n
elements in the input list.
An algorithm that processes a list of n elements The time complexity of this algorithm is
and performs O(log n) operations on each O(nlogn), since the number of operations
element.
performed is O(log n) on each element, and
there are n elements in the input list.

WAYS TO IMPROVE THE TIME COMPLEXITY OF AN ALGORITHM

1. Use a more efficient data structure, such as a hash table instead of a linked list.
2. Use a faster sorting algorithm, such as quicksort instead of bubblesort.
3. Use an algorithm with a better time complexity for the problem at hand, such as using
dynamic programming instead of brute force.
4. Optimize the algorithm for the specific input by using techniques such as memorization or
precomputation.

SEARCHING

➢ Linear search is an algorithm that takes in a list of values and a target value, and searches for
the target value in the list by iterating through each element in the list until it is found. The
time complexity of this algorithm is O(n) because the running time is directly proportional to
the size of the input list.
➢ Binary search is an algorithm that takes in a sorted list of values and a target value, and
searches for the target value in the list by repeatedly dividing the search space in half. The
time complexity of this algorithm is O(logn) because the running time increases
logarithmically with the size of the input list.

You might also like