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

fix(runtimes/02-binary-search):fixes binary search iterative#113

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

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 13 additions & 37 deletionssrc/runtimes/02-binary-search.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) {

if(current===search){
returnoffset+half;
}if(array.length===1){
}
if(array.length===1){
return-1;
}if(search>current){
}
if(search>current){
constright=array.slice(half);
returnbinarySearchRecursive(right,search,offset+half);
}
Expand All@@ -38,50 +40,24 @@ function binarySearchRecursive(array, search, offset = 0) {
*@param {string|number} search value to search for
*/
functionbinarySearchIterative(array,search){
// console.log('binarySearchIterative', {array, search});
letstart=0;
letend=array.length;
consthalf=()=>parseInt((end-start)/2,10)+start;
letend=array.length-1;
consthalf=()=>start+parseInt((end-start)/2,10);

while(end-start>0){
while(start<=end){
constcurrentIndex=half();
constcurrent=array[currentIndex];

if(current===search){
returncurrentIndex;
}if(search>current){
start=currentIndex;
if(current===search)returncurrentIndex;

if(search>current){
start=currentIndex+1;
}elseif(search<current){
end=currentIndex;
end=currentIndex-1;
}
}

return-1;
}

// const binarySearch = binarySearchRecursive;
constbinarySearch=binarySearchIterative;

// function test() {
// const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel',
// 'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia',
// 'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe'];
//
// const assert = require('assert');
// assert.equal(binarySearch([], 'not found'), -1);
// assert.equal(binarySearch([1], 2), -1);
// assert.equal(binarySearch([1], 1), 0);
// assert.equal(binarySearch([1, 2, 3], 1), 0);
// assert.equal(binarySearch([1, 2, 3], 2), 1);
// assert.equal(binarySearch([1, 2, 3], 3), 2);
// assert.equal(binarySearch([1, 2, 3], 31), -1);
// assert.equal(binarySearch(directory, 'Adrian'), 0);
// assert.equal(binarySearch(directory, 'Hanna'), 5);
// assert.equal(binarySearch(directory, 'Zoe'), 18);
// assert.equal(binarySearch(directory, 'not found'), -1);
// }

// test();


module.exports={ binarySearch, binarySearchIterative, binarySearchRecursive};
module.exports={ binarySearchIterative, binarySearchRecursive};
50 changes: 30 additions & 20 deletionssrc/runtimes/02-binary-search.spec.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
constbinarySearch=require('./02-binary-search').binarySearchRecursive;
const{
binarySearchRecursive,
binarySearchIterative,
}=require("./02-binary-search");

describe('Binary Search',()=>{
letarray;
constbinarySearchImplementations=[
binarySearchRecursive,
binarySearchIterative,
];

beforeEach(()=>{
array=[7,9,13,23];
});
binarySearchImplementations.forEach((binarySearchImpl)=>{
describe(binarySearchImpl.name,()=>{
letarray;

it('should find a middle element',()=>{
expect(binarySearch(array,9)).toEqual(1);
});
beforeEach(()=>{
array=[7,9,13,23];
});

it('should findan first element',()=>{
expect(binarySearch(array,7)).toEqual(0);
});
it("should finda middle element",()=>{
expect(binarySearchImpl(array,9)).toEqual(1);
});

it('should findthe last element',()=>{
expect(binarySearch(array,23)).toEqual(3);
});
it("should findan first element",()=>{
expect(binarySearchImpl(array,7)).toEqual(0);
});

it('should not find an bigger element',()=>{
expect(binarySearch(array,9000)).toEqual(-1);
});
it("should find the last element",()=>{
expect(binarySearchImpl(array,23)).toEqual(3);
});

it("should not find an bigger element",()=>{
expect(binarySearchImpl(array,9000)).toEqual(-1);
});

it('should find a smaller element',()=>{
expect(binarySearch(array,-9)).toEqual(-1);
it("should find a smaller element",()=>{
expect(binarySearchImpl(array,-9)).toEqual(-1);
});
});
});

[8]ページ先頭

©2009-2025 Movatter.jp