Selection sort algorithm

Selection sort algorithm in JavaScript

·

3 min read

Table of contents

No heading

No headings in the article.

Hi!

According to Google selection sort algorithm is the most popular one. So I tried to create function that will sort given array with selection sort algorithm.

How this algorithm work.

The principle is that we take first number, then loop throught array and look for any lower number. When we find lower number we swap our numbers.

arr = [ 2 , 5 , 1 , 6 , 9 ]

And then we continiue from second number in our array. Again we look for any lower number if we find one we swap them.

arr = [ 1 , 5 , 2 , 6 , 9 ]

After we finish iterating through our array we should be left with sorted array.

Code.

As for start for sure we will need two loops to iterate through our array. First loop will be responsible for iterating through whole array. Second loop will be responsible for iterating through elements to find our minimal number.

for(let j=0; j<array.length; j++){
    for(let i=j; i<array.length; i++){
    }
}

Here it is important that second loop does iterate through whole array. Because after our first iteration we will have lowest number as first element saved in our array. So then we need to start looking from second element.

Then we declare two variables that we will use to save lowest number and its position in array. We set our min number as first element of array, and its position to 0.

In our inside loop we check as we iterate if any number is lower than our current lowest number. If yes, we save it and its position as our new min nubmer.

function selectionSort(array){
  let min = array[0];
  let minposition = 0;

  for(let j=0; j<array.length; j++){
    for(let i=j; i<array.length; i++){
      if(array[i] < min) {
        min = array[i];
        minposition = i;
      }      
    }
  }

When we finish our inside loop, we should have lowest number and its position saved. Only step now is to swap them. And set new min number as second in our array.

function selectionSort(array){
  let min = array[0];
  let minposition = 0;
  let replace;

  for(let j=0; j<array.length; j++){
    for(let i=j; i<array.length; i++){
      if(array[i] < min) {
        min = array[i];
        minposition = i;
      }      
    }
    replace = array[j];
    array[minposition] = replace;    
    array[j] = min; 
    min = array[j+1];
  }  
  return array;
}

Refactoring.

To refactor this I can think only with replacing two numbers with more simple approach. I found Assignment Operator that will allow to swap two array elements without temporary variable.

So our final code will look like this.

function selectionSort(array){
  let min = array[0];
  let minposition = 0;

  for(let j=0; j<array.length; j++){
    for(let i=j; i<array.length; i++){
      if(array[i] < min) {
        min = array[i];
        minposition = i;
      }      
    }
    [array[j], array[minposition]] = [array[minposition], array[j]]
    min = array[j+1];
  }  
  return array;
}

As for my test I think everything is working as it should. If you find any mistakes or you have ideas how to refactor my solutions to be simplier/ better please let me know :)

Did you find this article valuable?

Support Chris by becoming a sponsor. Any amount is appreciated!