Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

remove unused var in function selectionSort()#6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
404pnf wants to merge1 commit intooreillymedia:master
base:master
Choose a base branch
Loading
from404pnf:patch-3

Conversation

404pnf
Copy link

selectionSort()

Original code example has an unused var temp. We can drop that var.

functionselectionSort(){varmin;for(varouter=0;outer<=this.dataStore.length-2;++outer){min=outer;for(varinner=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 if clause in inner for loop. I think this is better because otherwise even if outer and min is the same value, swap is called. It's a bit confusing.

functionselectionSort(){for(varouter=0;outer<=this.dataStore.length-2;++outer){for(varinner=outer+1;inner<=this.dataStore.length-1;++inner){if(this.dataStore[inner]<this.dataStore[outer]){swap(this.dataStore,inner,outer);}}}}

selectionSort()Original code example has an unused var temp. We can drop that var.````jsfunction 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.````jsfunction 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);         }      }   }}  ````
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@404pnf

[8]ページ先頭

©2009-2025 Movatter.jp