Created
June 9, 2018 12:36
-
-
Save kas-elvirov/f4e4c40ac69a6b3ca41886454dcac66e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Finds smallest element of an array | |
* | |
* @param {Array} arr array for searching | |
* | |
* @return {number} index of smallest element in array | |
*/ | |
const findSmallest = array => { | |
let currentValue = array[0]; | |
let currentIndex = 0; | |
let arrLen = array.length; | |
for (let i = 0; i < arrLen; i++) { | |
if (array[i] < currentValue) { | |
currentValue = array[i]; | |
currentIndex = i; | |
} | |
} | |
return currentIndex; | |
}; | |
/** | |
* Sorts an array of numbers | |
* | |
* @param {Array} arr An array of numbers | |
* | |
* @return {Array} New sorted array | |
*/ | |
const sort = array => { | |
let arr = Array.prototype.slice.call(array); | |
let arrLen = arr.length; | |
let newArr = []; | |
for (let i = 0; i < arrLen; i++) { | |
newArr.push(arr.splice(findSmallest(arr), 1)[0]); | |
} | |
return newArr; | |
}; | |
let array = [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]; | |
console.log(sort(array)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment