Created
June 20, 2021 22:40
-
-
Save ibrahimBanat/080d7f7fb125bd6b6144d382fd66c0bf to your computer and use it in GitHub Desktop.
This file contains 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
"use strict"; | |
/** | |
* takes an array of intgers as input and returns | |
* and array of these intgers in sorted order from least ot the largest | |
* @param {Array} array of intgers | |
* @returns Array of sorted intgers | |
*/ | |
function insertSort(array) { | |
if (array.length === 0) { | |
return "Exception"; | |
} | |
for (let item = 1; item < array.length; item++) { | |
for (let index = item; index > 0; index--) { | |
if (array[index] < array[index - 1]) { | |
const temp = array[index]; | |
array[index] = array[index - 1]; | |
array[index - 1] = temp; | |
} else { | |
break; | |
} | |
} | |
} | |
return array; | |
} | |
module.exports = insertSort; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment