Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion algorithms/searching/binary-search/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const elements = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
const elementToFind = 11;

const startTime = Date.now();
const startTime = Date.now();

function binarySearch(array, item) {
if (!Array.isArray(array)) return -1;
Expand Down Expand Up @@ -29,3 +29,5 @@ const elementFoud = binarySearch(elements, elementToFind);

console.log('Indice do elemento:', elementFoud);
console.log(`Tempo de execução: ${Date.now() - startTime}ms`);

module.exports = binarySearch;
6 changes: 3 additions & 3 deletions algorithms/searching/linear-search/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const elements = [2, 8, 3, 1, 5, 6, 7, 9, 10, 11, 15, 13, 12, 14];
const elementToFind = 11;

const startTime = Date.now();

function linearSeach(array, item) {
Expand All @@ -15,7 +14,8 @@ function linearSeach(array, item) {
return -1;
}

const elementFoud = linearSeach(elements, elementToFind);

const elementFoud = linearSeach(elements, elementToFind);
console.log('Elemento:', elementFoud);
console.log(`Tempo de execução: ${Date.now() - startTime}ms`);

module.exports = linearSeach;
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
languageOptions: {
globals: {
...globals.node,
...globals.jest,
},

ecmaVersion: 12,
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
preset: 'node',
//preset: 'node',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/'],
testMatch: ['**/*.test.js'],
Expand Down
45 changes: 45 additions & 0 deletions tests/algorithms/searching/binary-search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const binarySearch = require('../../../algorithms/searching/binary-search/index.js');

describe('Binary Search', () => {
it('alvo: 7, lista(1,3,5,7,9,11,13)', () => {
const array = [1, 3, 5, 7, 9, 11, 13];
const target = 7;
const result = binarySearch(array, target);
expect(result).toBe(3);
});

it('alvo: 13, lista(1,3,5,7,9,11,13)', () => {
const array = [1, 3, 5, 7, 9, 11, 13];
const target = 13;
const result = binarySearch(array, target);
expect(result).toBe(6);
});

it('alvo: 14, lista(1,3,5,7,9,11,13)', () => {
const array = [1, 3, 5, 7, 9, 11, 13];
const target = 14;
const result = binarySearch(array, target);
expect(result).toBe(-1);
});

it('alvo: 4, lista(1, 3, 5, 7, 9, 11, 13)', () => {
const array = [1, 3, 5, 7, 9, 11, 13];
const target = 4;
const result = binarySearch(array, target);
expect(result).toBe(-1);
});

it('alvo: 5, lista(5)', () => {
const array = [5];
const target = 5;
const result = binarySearch(array, target);
expect(result).toBe(0);
});

it('alvo: 1, lista(vazia)', () => {
const array = [];
const target = 1;
const result = binarySearch(array, target);
expect(result).toBe(-1);
});
});
31 changes: 31 additions & 0 deletions tests/algorithms/searching/linear-search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const linearSearch = require('../../../algorithms/searching/linear-search/index.js');

describe('Linear Search', () => {
it('alvo: 7, lista(1,3,5,7,9,11,13)', () => {
const array = [1, 3, 5, 7, 9, 11, 13];
const target = 7;
const result = linearSearch(array, target);
expect(result).toBe(3);
});

it('alvo: 4, lista(1,3,5,7,9,11,13)', () => {
const array = [1, 3, 5, 7, 9, 11, 13];
const target = 4;
const result = linearSearch(array, target);
expect(result).toBe(-1);
});

it('alvo: 5, lista(5)', () => {
const array = [5];
const target = 5;
const result = linearSearch(array, target);
expect(result).toBe(0);
});

it('alvo: 1, lista(vazia)', () => {
const array = [];
const target = 1;
const result = linearSearch(array, target);
expect(result).toBe(-1);
});
});