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

insertionSort(): var outer initialized as 1 or 0? #7

Open
@404pnf

Description

@404pnf

I think I found a duplicate prediction in the code.

Code example in the book, Chapter 12.

functioninsertionSort(){vartemp,inner;for(varouter=1;outer<=this.dataStore.length-1;++outer){temp=this.dataStore[outer];inner=outer;while(inner>0&&(this.dataStore[inner-1]>=temp)){this.dataStore[inner]=this.dataStore[inner-1];--inner;}this.dataStore[inner]=temp;}}

The var outer initialized as 1. Let's change it to 0. It doesn't affect the function.

Because the inner > 0 in while loop guards against out of bound error in this.dataStore(inner - 1).

Therefore, either we initialize outer with 0 or initialize outer with 1 but drop inner > 0 predict since we don't need it.

I prefer setting outer to 0 at the beginning.

functioninsertionSort(){vartemp,inner;for(varouter=0;outer<=this.dataStore.length-1;++outer){temp=this.dataStore[outer];inner=outer;while(inner>0&&(this.dataStore[inner-1]>=temp)){this.dataStore[inner]=this.dataStore[inner-1];--inner;}this.dataStore[inner]=temp;}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp