1
\$\begingroup\$

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

asked Jul 26, 2016 at 9:28
\$\endgroup\$
1
  • \$\begingroup\$ What if the array has negatives and zeros too? Instead of min as zero, use -Infinity. Also min === 0 would need to be changed. \$\endgroup\$ Commented May 7, 2022 at 5:42

2 Answers 2

4
\$\begingroup\$

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;
 }
answered Jul 26, 2016 at 10:15
\$\endgroup\$
0
2
\$\begingroup\$

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

answered Jul 26, 2016 at 10:53
\$\endgroup\$
3
  • \$\begingroup\$ Can't read. Could you, please, indent your code properly? \$\endgroup\$ Commented Jul 26, 2016 at 11:00
  • \$\begingroup\$ @Rahul Shivsharan you can up vote my answer if it was helpful. \$\endgroup\$ Commented 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\$ Commented Jul 29, 2016 at 10:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.