# Selection Sort - From Data Structures and Algorithm

Sorting an array of elements can be achieved in different ways. This article is on one of the ways called Selection Sort. Sorting helps in search algorithms like Linear, Binary, etc. Selection sort works by looking for the smallest element and swapping it with the first element in the array. And this is repeated for all the subsets until we reach the last index of the array. For example,

```
var array = [22, 12, 13, 4, 9, 23, 34, 19]
```

In the above array, in a loop, we need to find the smallest element and then swap two elements at each go to achieve a sorted array. So, we can divide this algorithm into three simple tasks:

- Find the minimum value and index of the minimum value.
- Swap two elements by using their indexes.
- Run the above two functions in a loop until the array is sorted and print the sorted array.

Let us begin with the simplest task first. Swapping two elements by using their indexes:

```
var swapping = function(array, firstIndex, secondIndex){
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
}
```

Naming the function and variables helps in writing easy to read code, like from the above code snippet. Here, we are taking array and two indexes from which two. numbers have to be swapped. We are copying the element from firstIndex of the array into a temporary variable, then we are replacing the first element with the element from second index of the array. And lastly we are putting the temporary value into second index of the array.

Moving on to finding the minimum value from each subset of the array. Let us write another function for this:

```
var findingIndexOfLeastValue = function(array, currentIndex){
var minimumValue = array[currentIndex];
var indexOfMinimumValue = currentIndex;
for(var i=currentIndex+1; i<array.length; i++){
if(array[i] < array[i+1]){ //comparing two elements
minimumValue = array[i];
indexOfMinimumValue = i;
}
}
return indexOfMinimumValue;
}
```

From the above code, the current index starts from 0 and moves on till end while we are updating the minimum value to that index place, from the subset of the array starting from currentIndex+1.

Let us now move on to the final function of sorting an array with the help of above two functions:

```
var selectionSort = function(array){
for(var i = 0; i < array.length; i++){
var indexOfMinimumValue = findingIndexOfLeastValue(array, i);
swapping(array, indexOfMinimumValue, i);
//here swapping the current position with least value
}
}
```

That's it! We are all good to try running the above function with different arrays to test if it works in all odd cases. Let me give you one example and you can try with more of them.

```
var array1 = [28, 12, 8, 23, 32, 9, 0, 15];
selectionSort(array1);
println("Array after sorting: " + array1);
```

It was fun revising this algorithm. Logics are always interesting. I hope this was helpful. Thank you for reading so far! Happy Coding :)