# Selection sort algorithm examination

I was asked Selection sort algorithm to program.

I coded it in javascript and shown to examiner.

But he was not satisfied, he said its too complicated.

I think I have written correctly.

this is the code below,

function selectionSort(inputArray){
var swapElementIndex = 0;
var shouldSwap = false;
var temp = 0;
var minNum = 0;
for(var i = 0;i < (inputArray.length - 1);i++){
minNum = 0;
for(var currentIndex = (i+1); currentIndex < inputArray.length; currentIndex++){

if(inputArray[i] > inputArray[currentIndex]){
if(minNum === 0){ // when first minimum element is found
swapElementIndex = currentIndex;
shouldSwap = true;
minNum = inputArray[swapElementIndex];
}else{// when we have an minimum element

/* further check whether min element is less than the new found [min] element,
if yes set it as min element.
*/
if(minNum > inputArray[currentIndex]){
minNum = inputArray[currentIndex];
swapElementIndex = currentIndex;
shouldSwap = true;
minNum = inputArray[swapElementIndex];
}
}
}
}

if(shouldSwap === true){
temp = inputArray[i];
inputArray[i] = inputArray[swapElementIndex];
inputArray[swapElementIndex] = temp;
shouldSwap = false;
}
}

return inputArray;
}

console.log(selectionSort([1,31,26,4,3,12]));
console.log(selectionSort([5,6,1,2,3,4]));


The output when I run is as follows,

rahul@rahul:~/myPractise/Algo$node sorting.js [ 1, 3, 4, 12, 26, 31 ] [ 1, 2, 3, 4, 5, 6 ] rahul@rahul:~/myPractise/Algo$


So you can see that its working fine.

Please tell me how can I improve my code

Yes, it could be simpler.

1. Special case for first value is not needed.
2. Some variables could be removed.
3. All variables initialization could be removed.

Below, you can see your code reviewed.

    function selectionSort(inputArray) {
for (var i = 0; i < (inputArray.length - 1); i++) {
var minNum = inputArray[i];
var swapElementIndex = i;
for (var currentIndex = i + 1; currentIndex < inputArray.length; currentIndex++) {
if (inputArray[currentIndex] < minNum) {
swapElementIndex = currentIndex;
minNum = inputArray[swapElementIndex];
}
}
if (i != swapElementIndex) {
var temp = inputArray[i];
inputArray[i] = inputArray[swapElementIndex];
inputArray[swapElementIndex] = temp;
}
}
return inputArray;
}


just to make your code similar, you could do this

function selectionSort(inputArray){
var minNum;
for( var i = 0; i <= inputArray.length-1; i++)
{
minNum = i;
for( var j = i+1; j < inputArray.length; j++)
{
if(inputArray[j] < inputArray[minNum])
{
minNum = j;
}
}
if(minNum != i ) {
[inputArray[i], inputArray[minNum]] = [inputArray[minNum], inputArray[i]];
}
}
return inputArray;
}
console.log(selectionSort([1,31,26,4,3,12]));

Rather than declaring those variables swapElementIndex, shouldSwap temp minNum you only need to declare minNum. Also swapping items requires additional variables, I used ECMAScript How to swap two variables in javascript- ECMAScript e.g [a, b] = [b, a]; to swap the items

• Can't read. Could you, please, indent your code properly? – CiaPan Jul 26 '16 at 11:00
• @Rahul Shivsharan you can up vote my answer if it was helpful. – Siobhan Jul 27 '16 at 6:17
• @TolaniJaiye-Tikolo, it is nice but I don't know if ok for the OP uses ECMAScript 2015 features such as the "destructive assignment". But I upvoted because I liked this feature. – rdllopes Jul 29 '16 at 10:41