This post covers the essentials of selection sort using javascript. Selection Sort is a low-efficiency sorting algorithms but is easy to implement and understand. We will use a simple array to demonstrate the concepts of Selection Sort before getting into code.

**Concept:**

In selection sort, we start by assuming that the first number in the array is the minimum. We then find out the smallest number in the array and if there is any number smaller than the first number, we exchange the numbers after one pass through the array. After this, the number in first position is smallest and array is sorted till position 1.

Next, we assume that the number at second position is the smallest in the remainder of the array. We pass through the array comparing each number with this number. After the pass is complete, we exchange this number with the smallest number in the remainder of the array. At the end of this, we will have first and second number in the array in sorted order. We repeat this step for rest of the numbers until all the numbers are sorted.

Let’s understand this using the images below:

**Step 1: Initial Array**

**Step 2:**

We assume that the number at first position is the smallest number. We compare this number with the number at position 2.

Since 2 < 3, 2 is still the minimum number.

**Step 3:**

Compare the current minimum with the next number in the array. Still, 2 < 5. Current minimum remain as 2.

**Step 4:**

This time, 2 > 1, so we change the minimum number position to be 3 (index for number 1) as:

Now that we have a new number which is smallest, we will use this number for comparing with the rest of the array elements. Our aim is to find the smallest number in the array.

**Step 5:**

Since 1 < 4, 1 is the smallest number in the array. We exchange the initial minimum number (number at first position) with the smallest number (i.e 1) as follows:

This completes Pass 1 of sorting the array and at the end of Pass 1, we have the smallest number in the array at first position. Next, we need to find the smallest number in remainder of the array and exchange that with the number in the second position. We again assume that the number at position 2 is the smallest number and repeat the steps 1 to 5 above. At the end of Pass 2, we will have:

**Pass 2:**

Pass 2

**Pass 3:**

Similarly, after pass 3:

Pass 3

**Pass 4:**

Pass 4

Finally, after all the passes, we will have the sorted array as follows:

**Pass 5:**

Pass 5

**Complexity:**

Complexity of Selection Sort: O(N²)

**Selection Sort in Javascript**

Implementing Selection Sort is very straight-forward once the concept is clear. Following function takes an array as argument and sort the content using selection sort.

```
function selectionSort(items) {
var length = items.length;
for (var i = 0; i < length - 1; i++) {
//Number of passes
var min = i; //min holds the current minimum number position for each pass; i holds the Initial min number
for (var j = i + 1; j < length; j++) { //Note that j = i + 1 as we only need to go through unsorted array
if (items[j] < items[min]) { //Compare the numbers
min = j; //Change the current min number position if a smaller num is found
}
}
if (min != i) {
//After each pass, if the current min num != initial min num, exchange the position.
//Swap the numbers
var tmp = items[i];
items[i] = items[min];
items[min] = tmp;
}
}
}
```

**Conclusion:**

You would not use selection Sort for sorting an array in your day to day code. Instead, use `items.sort()`

from the JavaScript inbuilt Array method. The aim of this post was to illustrate the Selection Sort Technique.

**Practice Exercise:**

- Write a program in JavaScript to sort following list in ascending order using Selection Sort Algorithm.var nums = [34, 23, 12, 45, 9, 1, 24];
- Sort the list present in Q1 in descending order.
- Sort the following array of Persons in ascending order of ‘age’ using Selection Sort.var persons = [{"name": "john","age": "23"},{"name": "harry","age": "21"},{"name": "jack","age": "25"}];