# What is the time complexity for selection sort?

## What is the time complexity for selection sort?

In computer science, selection sort is an in-place comparison sorting algorithm. It has an O(n2) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort.

What is the worst-case running time for a selection sort?

O(n²)
Time and Space Complexity If we have n values in our array, Selection Sort has a time complexity of O(n²) in the worst case.

### What is selection sort in C++?

Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list.

What is the runtime complexity of the following code?

Code Time complexity
sum = 0 O(1)
for (i=1; I <= n; i*=2) O(logn) because I is incremented exponentially and loop will run for less number of times than n.
for(j=1; j<=n; j++) O(n) because j is incremented linearly and loop will run for n number of times.
sum++ O(1)

## Why is selection sort unstable?

Selection sort works by finding the minimum element and then inserting it in its correct position by swapping with the element which is in the position of this minimum element. This is what makes it unstable.

How do you calculate runtime complexity?

For any loop, we find out the runtime of the block inside them and multiply it by the number of times the program will repeat the loop. All loops that grow proportionally to the input size have a linear time complexity O(n) . If you loop through only half of the array, that’s still O(n) .

### Which sorting algorithm has best time complexity?

Bubble sort and Insertion sort – Best case time complexity: n when array is already sorted. Worst case: when the array is reverse sorted.

What is the disadvantage of selection sort?

What is the disadvantage of selection sort? Explanation: As the input size increases, the performance of selection sort decreases. Explanation: Since the input array is not sorted, bubble sort takes 5 iterations and selection sort takes 4(n-1) iterations.

## Is selection sorting stable?

NoSelection sort / Stable