Skip to content

Commit d66ecce

Browse files
Nirajkashyapmgechev
authored andcommitted
selectionsort - decrease outer loop iteration and remove extra variable (#161)
* decrease outer loop iteration. do not iterate for last element as it is last one and no comarison left * remove extra variable * add debug config for vscode * remove comment * remove lint error * remove vscode config file * Bump js-yaml from 3.13.0 to 3.13.1 (#160) Bumps [js-yaml](https://github.com/nodeca/js-yaml) from 3.13.0 to 3.13.1. - [Release notes](https://github.com/nodeca/js-yaml/releases) - [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md) - [Commits](nodeca/js-yaml@3.13.0...3.13.1) Signed-off-by: dependabot[bot] <[email protected]>
1 parent fde6fd8 commit d66ecce

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
2+
.vscode
23
npm-debug.log
34
debug
45
dist

src/sorting/selectionsort.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@
2525
*/
2626
var selectionSort = function (array, cmp) {
2727
cmp = cmp || compare;
28-
var min;
2928
var idx;
3029
var temp;
31-
for (var i = 0; i < array.length; i += 1) {
30+
for (var i = 0; i < array.length - 1; i += 1) {
3231
idx = i;
33-
min = array[i];
3432
for (var j = i + 1; j < array.length; j += 1) {
35-
if (cmp(min, array[j]) > 0) {
36-
min = array[j];
33+
if (cmp(array[idx], array[j]) > 0) {
3734
idx = j;
3835
}
3936
}
4037
temp = array[i];
41-
array[i] = min;
38+
array[i] = array[idx];
4239
array[idx] = temp;
4340
}
4441
return array;

0 commit comments

Comments
 (0)