bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/DSA/Time Complexity
DSA•Time Complexity

DSA Selection Sort Time Complexity

Native lesson simulator

Selection sort

DSA Selection Sort Time Complexity

Selection sort701219211334

Selection sort repeatedly picks the smallest remaining value.

Flash cards

Review the key moves

1/3
Core idea

What is the main idea behind DSA Selection Sort Time Complexity?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Order

Put the learning moves in the order that makes the concept easiest to apply.

Selection Sort Simulation
Selection Sort Time Complexity
DSA Selection Sort Time Complexity

See this page for a general explanation of what time complexity is.

Selection Sort Time Complexity

The Selection Sort algorithm goes through all elements in an array, finds the lowest value, and moves it to the front of the array, and does this over and over until the array is sorted.

Selection Sort goes through an array of n values n-1 times. This is because when the algorithm has sorted all values except the last, the last value must also be in its correct place.

The first time the algorithm runs through the array, every value is compared to find out which one is the lowest.

The second time the algorithm runs through the array, every value except the first value is compared to find out which is the lowest.

And in this way the unsorted part of the array becomes shorter and shorter until the sorting is done. So on average, (n) / (2) elements are considered when the algorithm goes through the array finding the lowest value and moving it to the front of the array.

In addition to all the compares needed, the number of swap considerings needed is n -1.

We can start calculating the number of operations for the Selection Sort algorithm:

Operations = (n - 1)· (n) / (2) + (n - 1)= (n2) / (2) - (n) / (2) + (n - 1)= (n2) / (2) + (n) / (2)

When looking at the run time for algorithms, we look at very large data sets, meaning n is a very big number. And for a very big number n, the term (n^2) / (2) becomes so much bigger than the term (n) / (2) that we can approximate by simply removing that second term (n) / (2).

Operations = (n2) / (2) + (n) / (2) ≈ (n2) / (2) = (1) / (2) · n2

Using Big O notation to describe the time complexity for the Selection Sort algorithm, we get:

O((1) / (2) · n2) = O(n2)

And time complexity for the Selection Sort algorithm can be displayed in a graph like this:

As you can see, the run time is the same as for Bubble Sort: The run time increases really fast when the size of the array is increased.

Selection Sort Simulation

Run the simulation for different number of values in an array, and see how the number of operations Selection Sort needs on an array of n elements is O(n^2):

The most significant difference from Bubble sort that we can notice in this simulation is that best and worst case is actually almost the same for Selection Sort (O(n^2)), but for Bubble Sort the best case runtime is only O(n).

The difference in best and worst case for Selection Sort is mainly the number of swaps. In the best case scenario Selection Sort does not have to swap any of the values because the array is already sorted. And in the worst case scenario, where the array already sorted, but in the wrong order, so Selection Sort must do as many swaps as there are values in the array.

Previous

DSA Bubble Sort Time Complexity

Next

DSA Insertion Sort Time Complexity