5/20/26, 11:25 AM DSA Selection Sort
Tutorials References Exercises Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
DSA Selection Sort
❮ Previous Next ❯
Selection Sort
The Selection Sort algorithm finds the lowest value in an array and moves it to the front of the
array.
Speed:
Selection Sort
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 1/14
5/20/26, 11:25 AM DSA Selection Sort
The algorithm looks through the array again and again, moving the next lowest values to the
Tutorials
front, until
the array References
is sorted. Exercises Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
How it works:
1. Go through the array to find the lowest value.
2. Move the lowest value to the front of the unsorted part of the array.
3. Go through the array again as many times as there are values in the array.
Continue reading to fully understand the Selection Sort algorithm and how to implement it
yourself.
REMOVE ADS
Manual Run Through
Before we implement the Selection Sort algorithm in a programming language, let's manually run
through a short array only one time, just to get the idea.
Step 1: We start with an unsorted array.
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 2/14
5/20/26, 11:25 AM DSA Selection Sort
[ 7, 12, 9, 11, 3]
Tutorials References Exercises Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
Step 2: Go through the array, one value at a time. Which value is the lowest? 3, right?
[ 7, 12, 9, 11, 3]
Step 3: Move the lowest value 3 to the front of the array.
[ 3, 7, 12, 9, 11]
Step 4: Look through the rest of the values, starting with 7. 7 is the lowest value, and already at
the front of the array, so we don't need to move it.
[ 3, 7, 12, 9, 11]
Step 5: Look through the rest of the array: 12, 9 and 11. 9 is the lowest value.
[ 3, 7, 12, 9, 11]
Step 6: Move 9 to the front.
[ 3, 7, 9, 12, 11]
Step 7: Looking at 12 and 11, 11 is the lowest.
[ 3, 7, 9, 12, 11]
Step 8: Move it to the front.
[ 3, 7, 9, 11, 12]
Finally, the array is sorted.
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 3/14
5/20/26, 11:25 AM DSA Selection Sort
Tutorials below
Run the simulation References
to see the stepsExercises
above animated: Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
Selection Sort
[ 7, 12, 9, 11, 3 ]
Manual Run Through: What Happened?
We must understand what happened above to fully understand the algorithm, so that we can
implement the algorithm in a programming language.
Can you see what happened to the lowest value 3? In step 3, it has been moved to the start of the
array, where it belongs, but at that step the rest of the array remains unsorted.
So the Selection Sort algorithm must run through the array again and again, each time the next
lowest value is moved in front of the unsorted part of the array, to its correct position. The sorting
continues until the highest value 12 is left at the end of the array. This means that we need to run
through the array 4 times, to sort the array of 5 values.
And each time the algorithm runs through the array, the remaining unsorted part of the array
becomes shorter.
We will now use what we have learned to implement the Selection Sort algorithm in a
programming language.
Selection Sort Implementation
To implement the Selection Sort algorithm in a programming language, we need:
1. An array with values to sort.
2. An inner loop that goes through the array, finds the lowest value, and moves it to the front
of the array. This loop must loop through one less value each time it runs.
3. An outer loop that controls how many times the inner loop must run. For an array with n
values, this outer loop must run n − 1 times.
The resulting code looks like this:
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 4/14
5/20/26, 11:25 AM DSA Selection Sort
Tutorials
Example
References Exercises Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
my_array = [64, 34, 25, 5, 22, 11, 90, 12]
n = len(my_array)
for i in range(n-1):
min_index = i
for j in range(i+1, n):
if my_array[j] < my_array[min_index]:
min_index = j
min_value = my_array.pop(min_index)
my_array.insert(i, min_value)
print("Sorted array:", my_array)
Try it Yourself »
REMOVE ADS
Selection Sort Shifting Problem
The Selection Sort algorithm can be improved a little bit more.
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 5/14
5/20/26, 11:25 AM DSA Selection Sort
In the code above, the lowest value element is removed, and then inserted in front of the array.
Tutorials References Exercises Get Certified Sign In
Each time the next lowest value array element is removed, all following elements must be shifted
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
one place down to make up for the removal.
These shifting operation takes a lot of time, and we are not even done yet! After the lowest value
(5) is found and removed, it is inserted at the start of the array, causing all following values to
shift one position up to make space for the new value, like the image below shows.
Note: You will not see these shifting operations happening in the code if you are using a high
level programming language such as Python or Java, but the shifting operations are still
happening in the background. Such shifting operations require extra time for the computer to do,
which can be a problem.
Solution: Swap Values!
Instead of all the shifting, swap the lowest value (5) with the first value (64) like below.
We can swap values like the image above shows because the lowest value ends up in the correct
position, and it does not matter where we put the other value we are swapping with, because it is
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 6/14
5/20/26, 11:25 AM DSA Selection Sort
not sorted yet.
Tutorials References Exercises Get Certified Sign In
Here is a simulation that shows how this improved Selection Sort with swapping works:
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
Speed:
Selection Sort
Here is an implementation of the improved Selection Sort, using swapping:
Example
my_array = [64, 34, 25, 12, 22, 11, 90, 5]
n = len(my_array)
for i in range(n):
min_index = i
for j in range(i+1, n):
if my_array[j] < my_array[min_index]:
min_index = j
my_array[i], my_array[min_index] = my_array[min_index], my_array[i]
print("Sorted array:", my_array)
Try it Yourself »
Selection Sort Time Complexity
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 7/14
5/20/26, 11:25 AM DSA Selection Sort
For a general explanation of what time complexity is, visit this page.
Tutorials References Exercises Get Certified Sign In
For a more thorough and detailed explanation of Selection Sort time complexity, visit this page.
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
Selection Sort sorts an array of n values.
On average, about elements are compared to find the lowest value in each loop.
n
And Selection Sort must run the loop to find the lowest value approximately n times.
We get time complexity:
n
2
O( ⋅ n) = O(n )
2 –––––––
–––––––
The 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.
Run the simulation below for different sized arrays.
The red dashed line represents the theoretical time complexity O(n ). 2
Blue crosses appear when you run the simulation. The blue crosses show how many operations
are needed to sort an array of a certain size.
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 8/14
5/20/26, 11:25 AM DSA Selection Sort
Tutorials References
Set values:
Exercises
300
Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
Random
Worst Case
Best Case Operations: 0
10 Random
Run Clear
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 )), but for Bubble Sort the best
2
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 array.
DSA Exercises
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 9/14
5/20/26, 11:25 AM DSA Selection Sort
Tutorials References Exercises Get Certified Sign In
VA
Test
PHP
Yourself
HOW TO
WithC Exercises
[Link] C++ C# BOOTSTRAP REACT MYSQL JQUE
Exercise:
Using Selection Sort on this array:
[7,12,9,11,3]
To sort the values from left to right in an increasing (ascending) order.
What is the value of the LAST element after the first run through?
Submit Answer »
Start the Exercise
❮ Previous Sign in to track progress Next ❯
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 10/14
5/20/26, 11:25 AM DSA Selection Sort
Tutorials References Exercises Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
COLOR PICKER
REMOVE ADS
Python - Get Started - [Link]
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 11/14
5/20/26, 11:25 AM DSA Selection Sort
Tutorials References Exercises Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
PLUS SPACES
GET CERTIFIED FOR TEACHERS
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 12/14
5/20/26, 11:25 AM DSA Selection Sort
Tutorials
BOOTCAMPS
References Exercises
CONTACT US
Get Certified Sign In
VA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUE
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
AngularJS Reference
jQuery Reference
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 13/14
5/20/26, 11:25 AM DSA Selection Sort
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and
Tutorials References Exercises
privacy policy.
Get Certified Sign In
VA PHP Copyright
HOW TO 1999-2026
[Link] Refsnes
C Data.C++
All Rights Reserved.
C# W3Schools is PoweredREACT
BOOTSTRAP by [Link]. MYSQL JQUE
[Link] w w .w [Link]/dsa/dsa_algo_selectionsort.php 14/14