Nodejs Array Insertion Sort insertionSort()

Here you can find the source of insertionSort()

Method Source Code

/**/*from   ww w. j av  a2s  . co  m*/
 * Insertion Sort Javascript implementation
 *  - Best: n / Average: n^2 / Worst: n^2
 *  - Memory: 1 / Stable
 *
 * @author: Sebastian Kim
 */

Array.prototype.insertionSort = function() {
  for(var i = 1; i < this.length; i++) {
    var hole = i;
    while (hole > 0 && this[hole - 1] > this[i]) {
      hole--;
    }
    this.splice(hole, 0, this[i]);
    this.splice(i + 1, 1);
  }

  return this;
}

Related

  1. insertSort()
    Array.prototype.insertSort=function(){
           var left=[];
          var res=this.splice(0,1)[0];
        left.push(res);
        for(var i=0;i<this.length;i++){
              var cur=this[i];
            for(var j=left.length-1;j>=0;){
                 if(left[j]>cur){
                     j--;
    ...
    
  2. insertSort()
    Array.prototype.insertSort = function(){
      var len = this.length;
      var temp;
      for(var i = 1;i < len;i++){
        temp = this[i];
        if(this[i] < this[i-1]){
          for(var j = i-1;j > 0&&temp<this[j];j--){
            this[j+1] = this[j];
          this[j+1] = temp;
      return this;
    
  3. insertationSort()
    Array.prototype.insertationSort = function() {
        this.procedures=[]
        var compare = 0
        var exchange = 0
        var comparet = this.length * this.length / 4
        var exchanget = comparet
        for (var i = 1; i < this.length; i++) {
            for (var j = i; j > 0; j--) {
                compare++
    ...
    
  4. insertion(arr)
    Array.prototype.insertion = function(arr){
        var returnArr = [];
        for(var i = 0; i < arr.length; i++){
            if(i==0){
                returnArr.push(arr[i]);
            }else{
                if(arr[i]<returnArr[i-1]){
                    var j = i;
                    while(j!=0&&arr[i]<returnArr[j]){
    ...
    
  5. insertionSort()
    Array.prototype.insertionSort = function () {
      var i, j;
      var temp;
      for (i = 1; i < this.length; i++) {
        temp = this[i];
        for (j = i - 1; j >=0 && this[j] > temp; j--) {
          this[j + 1] = this[j];
        this[j + 1] = temp;
    ...
    
  6. 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;
    
  7. 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;
    
  8. 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');
    
  9. 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;
    };