From b5826a2df97bb1404a1548061f3a04e01c4227b6 Mon Sep 17 00:00:00 2001 From: 404 <404pnf@users.noreply.github.com> Date: Tue, 27 Jan 2015 11:27:07 +0800 Subject: [PATCH] remove unused var in function selectionSort() selectionSort() Original code example has an unused var temp. We can drop that var. ````js function selectionSort() { var min; for (var outer = 0; outer <= this.dataStore.length-2; ++outer) { min = outer; for (var inner = outer + 1; inner <= this.dataStore.length-1; ++inner) { if (this.dataStore[inner] < this.dataStore[min]) { min = inner; } } swap(this.dataStore, outer, min); } } ```` We can drop the var min by moving swap into the inner for loop. ````js function selectionSort() { for (var outer = 0; outer <= this.dataStore.length-2; ++outer) { for (var inner = outer + 1; inner <= this.dataStore.length-1; ++inner) { if (this.dataStore[inner] < this.dataStore[outer]) { swap(this.dataStore, inner, outer); } } } } ```` --- Chapter12/Chap12-6.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Chapter12/Chap12-6.js b/Chapter12/Chap12-6.js index 2e567d0..ae96a5a 100644 --- a/Chapter12/Chap12-6.js +++ b/Chapter12/Chap12-6.js @@ -1,13 +1,11 @@ + function selectionSort() { - var min, temp; for (var outer = 0; outer <= this.dataStore.length-2; ++outer) { - min = outer; for (var inner = outer + 1; inner <= this.dataStore.length-1; ++inner) { - if (this.dataStore[inner] < this.dataStore[min]) { - min = inner; + if (this.dataStore[inner] < this.dataStore[outer]) { + swap(this.dataStore, inner, outer); } } - swap(this.dataStore, outer, min); } }