Added more implementations (JS) (#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 * #69 Added search for LCS * #69 added levenstein algorithm * Removed excessive property * Removed excessive file * Removed excessive function calls * Removed excessive file * Removed excessive file * Renamed * Fixed indentation
This commit is contained in:
committed by
Aditya Bhargava
parent
a5fe9178dd
commit
0d5d0164ce
33
02_selection_sort/javascript/02_recursive_selection_sort.js
Normal file
33
02_selection_sort/javascript/02_recursive_selection_sort.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Finds smallest element of an aray
|
||||
* @param {Array} arr array for searching
|
||||
* @return {number} index of the smallest element in array
|
||||
*/
|
||||
const findSmallest = ( arr ) => {
|
||||
let smallest = arr[0];
|
||||
let smallestIndex = 0;
|
||||
let arrLen = arr.length;
|
||||
|
||||
for ( let i = 0; i < arrLen; i++ ) {
|
||||
if ( arr[i] < smallest ) {
|
||||
smallest = arr[i];
|
||||
smallestIndex = i;
|
||||
}
|
||||
}
|
||||
return smallestIndex;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sorts0 recursively an array of numbers
|
||||
* @param {Array} arr An array of numbers
|
||||
* @return {Array} New sorted array
|
||||
*/
|
||||
const selectionSort = ( arr ) => {
|
||||
if ( !arr.length ) return [];
|
||||
let smallest = arr.splice( findSmallest( arr ), 1 );
|
||||
return smallest.concat( selectionSort( arr ) );
|
||||
};
|
||||
|
||||
let arr = [5, 3, 6, 2, 10];
|
||||
|
||||
console.log( selectionSort(arr) );
|
||||
Reference in New Issue
Block a user