How to Sort in JavaScript using Selection Sort: Made Easy
Implement your selection sort algorithm: a full guide
Table of contents
This article is a part of my series on JavaScript sorting algorithms. I've written a blog about Bubble Sort as well, so check it out to add another sorting algorithm to your bag!
How does selection sort work?
Let's say we have a list/array of numbers as follows:
[14, 17, 8, 22, 25]
In selection sort, you loop through the array and select the smallest/largest (or whatever, you're the one selecting) element from the list. Also, you have a pointer in the array, which starts at the first element. The smallest element in our case is 8.
Next, we swap 8 with the element to which the pointer is pointing. Hence, we swap 14 and 8. The array is now as follows:
[8, 17, 14, 22, 25]
We continue to repeat this 2-step process till the pointer is at the last index. Check this link out if you want to have a look at a sweet insertion sort visualization.
Pseudocode
As you can guess, the pseudocode isn't that tough. Though, it's always necessary, to identify errors easier and map out our code better.
"Map out our code better". That's a great way to put it.
Start looping from the beginning of the array with a variable
i
till the end of the arrayCreate a variable called
lowest
or whatever you wanna name it. This variable will keep track of the index of the lowest element that we happen to find.Loop through the array till the end, this time with a variable
j
which equalsi+1
since we want to start looping right after the "lowest" elementCheck if array of
j
is more than array oflowest
If so, set
lowest
toj
After
j
's loop gets over, check ifi
is equal tolowest
. If this does equal true, we'll know that the value ofi
never changed (remember,i
was set tolowest
at the beginning). If it's not true, we swap array oflowest
with array ofi
.Return the sorted array
Seriously, that's a lot! I'd suggest you just think of a way to implement selection sort yourself if you want to challenge yourself and maximize your gains from this blog post. If not, let's look at the code in the next section.
Coding it out
function selectionSort(arr) {
const swap = (arr, idx1, idx2) => ([arr[idx1], arr[idx2]] = [arr[idx2], arr[idx1]]);
for (let i = 0; i < arr.length; i++) {
let lowest = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[lowest] > arr[j]) {
lowest = j;
}
}
if (i !== lowest) swap(arr, i, lowest);
}
return arr;
}
The function swap
inside of the main function uses some sweet ES2015 syntax to swap the array values using array destructuring and returns it. I must admit, the code is very short and will be confusing for beginners. However, it's concise.
I don't think I need to explain this code any more since the pseudocode I had was pretty much a recitation of this code (great job, Shivank).
If you liked this article, consider pressing the heart button to show some love. Drop a comment if you have some feedback for me. I value any feedback you have. Check out my article on bubble sort if you found this article helpful.
Thanks for reading!