Skip to content

Commit 0d5d016

Browse files
kas-elvirovegonSchiele
authored andcommitted
Added more implementations (JS) (egonSchiele#67)
* Added recursive binary search (JS) * Added recursive selection sorting * Added another loop implementation sum func by reduce (JS) * Recursion reduced by one iteration * Recursive binary search in ES6 added to appropriate folder * JS files ordered by standards (ES4/ES6) * Added hashtable implementation in JS * Fixed typo with LENGTH prop * Added Euclidian algorithm for two numbers and set of them * Added universal selection sort * Commented output * Added ES6 version of Euclidean algorithm * Converted from ES6 to ES5 * egonSchiele#69 Added search for LCS * egonSchiele#69 added levenstein algorithm * Removed excessive property * Removed excessive file * Removed excessive function calls * Removed excessive file * Removed excessive file * Renamed * Fixed indentation
1 parent a5fe917 commit 0d5d016

File tree

12 files changed

+503
-30
lines changed

12 files changed

+503
-30
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Searches recursively number from the list
3+
* @param {Array} list
4+
* @param {number} item Search item
5+
* @param {number} low Lower limit of search in the list
6+
* @param {number} high Highest limit of search in the list
7+
* @return {(number | null)} Number if the value is found or NULL otherwise
8+
*/
9+
const binarySearch = ( list, item, low, high ) => {
10+
let arrLength = list.length;
11+
while ( low <= high ) {
12+
let mid = Math.floor((low + high) / 2);
13+
let guess = list[mid];
14+
15+
if ( guess === item ) {
16+
return mid;
17+
} else if ( guess > item ) {
18+
high = mid - 1;
19+
list = list.slice( 0, mid );
20+
return binarySearch( list, item, low, high );
21+
} else {
22+
low = mid + 1;
23+
list = list.slice( low, arrLength );
24+
return binarySearch( list, item, low, high );
25+
}
26+
}
27+
28+
return null;
29+
};
30+
31+
/**
32+
* Creates the array that contains numbers 1...N
33+
* @param {number} n - number N
34+
* @return {Array}
35+
*/
36+
const createArr = ( n ) => Array.from({length: n}, (v, k) => k + 1);
37+
38+
const myList = createArr( 100 );
39+
let low = 0;
40+
let high = myList.length - 1;
41+
42+
console.log( binarySearch( myList, 3, low, high ) ); // 2
43+
console.log( binarySearch( myList, -1, low, high ) ); // null
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Finds smallest element of an aray
3+
* @param {Array} arr array for searching
4+
* @return {number} index of the smallest element in array
5+
*/
6+
const findSmallest = ( arr ) => {
7+
let smallest = arr[0];
8+
let smallestIndex = 0;
9+
let arrLen = arr.length;
10+
11+
for ( let i = 0; i < arrLen; i++ ) {
12+
if ( arr[i] < smallest ) {
13+
smallest = arr[i];
14+
smallestIndex = i;
15+
}
16+
}
17+
return smallestIndex;
18+
};
19+
20+
/**
21+
* Sorts0 recursively an array of numbers
22+
* @param {Array} arr An array of numbers
23+
* @return {Array} New sorted array
24+
*/
25+
const selectionSort = ( arr ) => {
26+
if ( !arr.length ) return [];
27+
let smallest = arr.splice( findSmallest( arr ), 1 );
28+
return smallest.concat( selectionSort( arr ) );
29+
};
30+
31+
let arr = [5, 3, 6, 2, 10];
32+
33+
console.log( selectionSort(arr) );

04_quicksort/ES6/01_loop_sum.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
const sum = (arr) => {
2-
let total = 0;
3-
for (let x = 0; x < arr.length; x += 1) {
4-
total += arr[x];
5-
}
6-
return total;
1+
/**
2+
* Sums values in array by loop "for"
3+
* @param {Array} arr Array of numbers
4+
* @return {number} Sum of the numbers
5+
*/
6+
const sumLoop = ( arr ) => {
7+
let result = 0;
8+
9+
for ( let i = 0; i < newArr.length; i++ ) {
10+
result += newArr[i];
11+
}
12+
13+
return result;
714
};
815

9-
console.log(sum([1, 2, 3, 4])); // 10
16+
let arr = [1, 2, 3, 4];
17+
18+
console.log( sumLoop( arr ) ); // 10
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Sums values in array by function "reduce"
3+
* @param {Array} arr Array of numbers
4+
* @return {number} Sum of the numbers
5+
*/
6+
const sumReduce = ( arr ) => {
7+
let result = newArr.reduce( ( curr, prev ) => {
8+
return curr + prev;
9+
} );
10+
11+
return result;
12+
};
13+
14+
let arr = [1, 2, 3, 4];
15+
16+
console.log( sumReduce( arr ) ); // 10
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Recursive function of Euclidean algorithm for two numbers
3+
*
4+
* @param {number} a first number
5+
* @param {number} b second number (base case)
6+
*
7+
* @return {number} GCD (greatest common divisor)
8+
*/
9+
let gcdOfTwo = ( a, b ) => {
10+
if ( !b ) {
11+
return a;
12+
}
13+
return gcdOfTwo( b, a % b );
14+
};
15+
16+
/**
17+
* Recursive function of Euclidean algorithm for set of the numbers
18+
*
19+
* @param {Array} set Set of the numbers
20+
*
21+
* @return {number} GCD (greatest common divisor)
22+
*/
23+
let gcdOfSet = ( set ) => {
24+
let result = set[0];
25+
let newArr = Array.prototype.slice.call( set, 1 );
26+
27+
newArr.map( ( el ) => {
28+
result = gcdOfTwo( result, el );
29+
} );
30+
31+
return result;
32+
};
33+
34+
const set = [1680, 640, 3360, 160, 240, 168000];
35+
36+
console.log( gcdOfSet( set ) ); // 80
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Recursive function of Euclidean algorithm
3+
*
4+
* @param {number} a first number
5+
* @param {number} b second number (base case)
6+
*
7+
* @return {number} GCD (greatest common divisor)
8+
*/
9+
let getGCD = ( a, b ) => {
10+
if ( !b ) {
11+
return a;
12+
}
13+
return getGCD( b, a % b );
14+
};
15+
16+
const a = 1680;
17+
const b = 640;
18+
19+
console.log( getGCD( a, b ) ); // 80
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
'use strict';
2-
3-
function sum(arr) {
4-
let total = 0;
5-
for (let x = 0; x < arr.length; x++) {
6-
total += arr[x];
7-
}
8-
return total;
1+
/**
2+
* Sums values in array by loop "for"
3+
* @param {Array} arr Array of numbers
4+
* @return {number} Sum of the numbers
5+
*/
6+
function sumLoop( arr ) {
7+
var result = 0;
8+
9+
for ( var i = 0; i < newArr.length; i++ ) {
10+
result += newArr[i];
11+
}
12+
13+
return result;
914
}
1015

11-
console.log(sum([1, 2, 3, 4])) // 10
16+
var arr = [1, 2, 3, 4];
17+
18+
console.log( sumLoop( arr ) ); // 10
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Sums values in array by function "reduce"
3+
* @param {Array} arr Array of numbers
4+
* @return {number} Sum of the numbers
5+
*/
6+
function sumReduce( arr ) {
7+
var result = newArr.reduce( ( curr, prev ) => {
8+
return curr + prev;
9+
} );
10+
11+
return result;
12+
}
13+
14+
var arr = [1, 2, 3, 4];
15+
16+
console.log( sumReduce( arr ) ); // 10
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
'use strict';
1+
const arr = [1, 2, 3, 4];
22

3-
function sum(list) {
4-
if (list.length === 0) {
5-
return 0;
6-
}
7-
return list[0] + sum(list.slice(1));
8-
}
3+
/**
4+
* Sums values in array recursively
5+
* @param {Array} arr Array of numbers
6+
* @return {number} Sum of the numbers
7+
*/
8+
const sumRecursive = ( arr ) => {
9+
if ( arr.length == 1 ) return arr[0];
10+
return arr[0] + sumRecursive( arr.slice( 1 ) );
11+
};
912

10-
console.log(sum([1, 2, 3, 4])) // 10
13+
console.log( sumRecursive( arr ) ); // 10

0 commit comments

Comments
 (0)