Skip to content

Instantly share code, notes, and snippets.

@ibrahimBanat
Created June 20, 2021 22:25
Show Gist options
  • Save ibrahimBanat/f7f609a1ea7d60a63467ae790afa69ea to your computer and use it in GitHub Desktop.
Save ibrahimBanat/f7f609a1ea7d60a63467ae790afa69ea to your computer and use it in GitHub Desktop.
function insertSort(array) {
//creating insertion sort function
}
function insertSort(array) {
for (let item = 1; item < array.length; item++) {
//looping through the input list
}
}
function insertSort(array) {
for (let item = 1; item < array.length; item++) {
for (let index = item; index > 0; index--) {
//looping through the sub-sorted array down
//to the first eleemnt
}
}
}
function insertSort(array) {
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;
}
}
}
}
function insertSort(array) {
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;
}
}
}
}
function insertSort(array) {
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;
}
"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) {
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