Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

insertion sort javascript

let insertionSort = (inputArr) => {
    for (let i = 1; i < inputArr.length; i++) {
        let key = inputArr[i];
        let j = i - 1;
        while (j >= 0 && inputArr[j] > key) {
            inputArr[j + 1] = inputArr[j];
            j = j - 1;
        }
        inputArr[j + 1] = key;
    }
    return inputArr;
};
Comment

javascript insertion sort

const insertionSort = array => {
  const arr = Array.from(array); // avoid side effects
  for (let i = 1; i < arr.length; i++) {
    for (let j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
      [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]];
    }
  }
  return arr;
};

console.log(insertionSort([4, 9, 2, 1, 5]));
Comment

javascript insertion sort

function insertionSort(arr, compare = defaultCompare) {
    const { length } = arr;
    let temp;
    for (let i = 1; i < length; i++) {
        let j = i;
        temp = arr[i];
        while (j > 0 && compare(arr[j - 1], temp) === Compare.BIGGER_THAN) {
            arr[j] = arr[j - 1];
            j--;
        }
        arr[j] = temp;
    }
    return arr;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: delete node from linked list 
Javascript :: js array find 
Javascript :: javascript training 
Javascript :: onfocus 
Javascript :: react controlled input 
Javascript :: how to split an array into two javascript 
Javascript :: obfuscation js 
Javascript :: copy object with new property in js 
Javascript :: mongoose select 
Javascript :: crypto in node js 
Javascript :: componentdidmount in functional component 
Javascript :: sort numbers in array in js 
Javascript :: react white screen 
Javascript :: react usememo 
Javascript :: how to find a name of class from page in jquery 
Javascript :: how to cast in javascript 
Javascript :: Object.Values () javascript 
Javascript :: angular.toJson 
Javascript :: js filter 
Javascript :: javascript color green to red 
Javascript :: how to check for unused dependencies in my react project 
Javascript :: react chart js 2 api data 
Javascript :: javascript the event loop 
Javascript :: how to copy array of objects in javascript 
Javascript :: clearinterval in javascript 
Javascript :: decode raw data to string nodejs 
Javascript :: how to make a circle in p5js 
Javascript :: setstate in react 
Javascript :: How To Generate a Table With JavaScript 
Javascript :: add word in string in javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =