Nodejs Array Insertion Sort insertSort()

Here you can find the source of insertSort()

Method Source Code

Array.prototype.insertSort = function () {
    'use strict';

    var lastIndex = this.length,
        temp,/*  www .  j  a v  a 2 s . c o  m*/
        i,
        j;

    for ( i = 1 ; i < lastIndex ; i++ ) {

        for( j = 0 ; j < i ; j++ ){
            
            if( this[i] < this[j] ){
                temp = this[i];
                this[i] = this[j];
                this[j] = temp;
            }

        }   

    }

    return this;
};

var vector = [50,6, 2, 5, 23, 9, 17, 29, 3];

console.log(vector);

console.log(vector.insertSort());

Related

  1. insertSort()
    Array.prototype.insertSort = function() {
      for(var i = 1; i < this.length; i++) {
        var temp = this[i];
        var inp = i;
        while(inp > 0 && temp < this[inp - 1]){
          this[inp] = this[inp - 1];
          inp--;
        this[inp] = temp;
    ...
    
  2. 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--;
    ...
    
  3. 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;
    
  4. 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++
    ...