Nodejs Array Insertion Sort insertionSort (array)

Here you can find the source of insertionSort (array)

Method Source Code

// If comparator(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
// If comparator(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.
// If comparator(a, b) is greater than 0, sort b to a lower index than a.

// If no comparator is given, just sort the elements using < or >.
//Examples://from   www .j a  va2 s  .  c  o m
// Input   Output
// array:
// [ { "value": 3 }, { "value": 1 }, { "value": 2 } ]   [ { "value": 1 }, { "value": 2 }, { "value": 3 } ]
// array:
// [ { "value": 10 }, { "value": 5, "order": 1 }, { "value": 5, "order": 2 } ]   [ { "value": 5, "order": 1 }, { "value": 5, "order": 2 }, { "value": 10 } ]

Array.prototype.sort = function(){
  console.log("Please don't use the built in sort function.")
}

function insertionSort (array) {
  let current, i, j;
  
  for (i in array) {
    current = array[i];
    for (j=i-1; j>-1 && (array[j] > current); j--) {
      array[j+1] = array[j];
    }
    array[j+1] = current;
  }
  return array;
}

Related

  1. insertionSort()
    'use strict';
    const ArraySorting = {};
    ArraySorting.insertionSort = a => {
        for(let o = 1; o < a.length; o++) {
            for(let i = o; i > 0 && a[i] < a[i-1]; i--) {
                [a[i], a[i-1]] = [a[i-1], a[i]]
    Array.prototype.insertionSort = function () {
        ArraySorting.insertionSort(this);
    exports.ArraySorting = ArraySorting;
    
  2. insertionSort()
    Array.prototype.insertionSort = function(){
        if(this.length === 0)
            return "Array is empty!!";
        else if(this.length ===1)
            return this;
        else{
            for(var outerLoopIndex =1,arrLen = this.length;
                outerLoopIndex<arrLen;outerLoopIndex++)
                var temp = this[outerLoopIndex];
                for(var innerLoopIndex = outerLoopIndex-1;
                    innerLoopIndex >= 0 && this[innerLoopIndex] >                          temp ;innerLoopIndex--)
                    this[innerLoopIndex+1] = this[innerLoopIndex];   
                this[innerLoopIndex+1] = temp;
        return this;
    
  3. insertionSort()
    "use strict";
    Array.prototype.insertionSort = function() {
      for (var key = 1; key < this.length; key++) {
        for (var swap_key = key; this[swap_key] < this[swap_key - 1]; swap_key--) {
          var temp = this[swap_key];
          this[swap_key] = this[swap_key - 1];
          this[swap_key - 1] = temp;
    };
    var assert = function(expression, name) {
      if ( ! expression) {
        console.log('** Assertion failed', name || '')
    var a1 = [5,4,3,6,7,1,2,8,9];
    a1.insertionSort();
    console.log(a1);
    assert(a1.toString() === [1,2,3,4,5,6,7,8,9].toString(), 'Test it is sorted');
    
  4. insertionSort()
    Array.prototype.insertionSort = function () {
        for (var i = 1; i < this.length; i++) {
            var j = i, value = this[i];
            while(j > 0 && value < this[j-1]){
                this[j] = this[j-1];
                j--;
            this[j] = value;
    };
    
  5. insertionSort(desc)
    Array.prototype.insertionSort = function(desc) {
      let A = Object.assign([], this)
      for(let j = 1; j < A.length; j++) {
        let key = A[j]
        let i = j - 1
        while(i >= 0 && (desc ? A[i] < key : A[i] > key)) {
          A[i + 1] = A[i]
          i = i - 1
        A[i + 1] = key
      return A
    let arr = [3, 8, 1, 6, 5, 4, 9, 2, 7]
    let ascending = arr.insertionSort() 
    let descending = arr.insertionSort(true) 
    Array.prototype.selectionSort = function(desc) {
      let A = Object.assign([], this)
      for(let j = 0; j < A.length; j++) {
        let key = A[j]
        let m = j
        for(let i = j; i < A.length; i++) {
          m = (desc ? A[i] > A[m] : A[i] < A[m]) ? i : m
        A[j] = A[m]
        A[m] = key
      return A
    let arr = [3, 8, 1, 6, 5, 4, 9, 2, 7]
    let ascending = arr.selectionSort() 
    let descending = arr.selectionSort(true) 
    
  6. insertion_sort(array)
    'use strict';
    Array.prototype.swap = function swap (a, b) {
      const temp = this[a];
      this[a] = this[b];
      this[b] = temp;
    };
    function insertion_sort (arr) {
      for (let i = 0; i < arr.length; i++) {
        let j = i;
    ...