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 :: countTo add commas to number jquery 
Javascript :: jquery count elements 
Javascript :: isogram javascript 
Javascript :: chart.js hide bar title 
Javascript :: execute code after page load javascript 
Javascript :: how to view sync storage in chrome 
Javascript :: jquery if screen size 
Javascript :: js get number from string 
Javascript :: how to fix cors in angular 
Javascript :: javascript open link with button 
Javascript :: get domain name javascript 
Javascript :: jquery remove all tr from table 
Javascript :: sleep sort 
Javascript :: javascript substring last character 
Javascript :: moment format 23 hour 
Javascript :: jquery set style background image 
Javascript :: has decimal javascript 
Javascript :: javascript if undefined 
Javascript :: javascript truncate with ellipsis 
Javascript :: ajax csrf token laravel 
Javascript :: websocket sample code js 
Javascript :: check object has value 
Javascript :: how to center a canvas in javascript 
Javascript :: array to comma separated list js 
Javascript :: blob to file javascript 
Javascript :: javascript react reverse map 
Javascript :: express js basic example 
Javascript :: parsley cdn 
Javascript :: javascript change image src 
Javascript :: fibonacci sequence in javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =