In this article, we will explain the idea behind Selection Sort and implement it in JavaScript and how to work Selection Short in JavaScript.

Selection sort is the easiest and most intuitive sorting algorithm and is a comparison algorithm.

If you want then buy a good, reliable, secure web hosting service  from here: click here

Now, we can say that it transforms the input collection without using any auxiliary data structure and the input is overridden by the output.

Previous JavaScript Articles

Basically, it’s usually taught early in most computer science courses, because it’s so easy to understand and translate into code.

Despite its quadratic time complexity, it has performance advantages over more complex sorting algorithms in certain cases, such as Quicksort or Merge Sort.

How to write sort algorithm in JavaScript

Selection Sort

Hence, the algorithm splits the input array into two sublists – a sorted and an unsorted sublist.

You can purchase your hosting from Cloudsurph.comCloudsurph hosting is a reliable hosting option for business and personal projects. We offer insight and help on system configuration issues and code errors or bugs.

So, in the beginning, the sorted list is located in the collection and all elements to the right of the final sorted element are treated as unsorted.

If you want then buy a good, reliable, secure web hosting service  from here: click here
function selectionSort(arr) {
const len = arr.length;
for (let i = 0; i < len - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
if (minIndex !== i) {
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
return arr;

How to work Selection Short in JavaScript

  1. The function selectionSort takes an array arr as input.
  2. The length of the array is stored in the variable lane.
  3. The outer loop iterates through the array from the first element to the second-to-last element.
  4. The variable minIndex is set to the current index i.
  5. The inner loop iterates through the array from the element after the current index to the last element.
  6. If the current element is less than the element at the minIndex, the minIndex is set to the index of the current element.
  7. If minIndex is not equal to i, then the elements at indices i and minIndex are swapped.
  8. Once the outer loop completes, the sorted array is returned.

Note that this implementation modifies the input array in place rather than returning a new array.


Selection sort is a basic and simple, therefore inefficient sorting algorithm. It served as a basis for some of the widely used and adopted algorithms and isn’t used very often in the development industry.

If you want then buy a good, reliable, secure web hosting service  from here: click here

That’s it. If you enjoyed reading this article and have more questions please reach out to our support team via live chat or email and we would be glad to help you. we provide server hosting for all types of need and we can even get your server up and running with the service of your choice.