I had an algorithm test.
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
2 Answers 2
Yes, it could be simpler.
- Special case for first value is not needed.
- Some variables could be removed.
- 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
-
\$\begingroup\$ Can't read. Could you, please, indent your code properly? \$\endgroup\$CiaPan– CiaPan2016年07月26日 11:00:41 +00:00Commented Jul 26, 2016 at 11:00
-
\$\begingroup\$ @Rahul Shivsharan you can up vote my answer if it was helpful. \$\endgroup\$Tolani– Tolani2016年07月27日 06:17:39 +00:00Commented Jul 27, 2016 at 6:17
-
\$\begingroup\$ @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. \$\endgroup\$rdllopes– rdllopes2016年07月29日 10:41:38 +00:00Commented Jul 29, 2016 at 10:41
min === 0
would need to be changed. \$\endgroup\$