Nodejs Array Sort countingSort()

Here you can find the source of countingSort()

Method Source Code

Array.prototype.countingSort = function() {
   var maxDigit = this[0];
   for(var i = 1; i < this.length; i++) {
      if(this[i] > maxDigit) {
         maxDigit = this[i];/*w  w  w  .  jav a 2s . c o m*/
      }
   }

   var bucket = new Array(maxDigit + 1);

   for(var i = 0; i < this.length; i++) {
      if(!bucket[this[i]]) {
         bucket[this[i]] = 0;
      }
      bucket[this[i]]++;
   }

   var index = 0

   for(var i = 0; i < bucket.length; i++) {
      while(bucket[i] > 0) {
         this[index++] = i;
         bucket[i]--;
      }
   }
}

var b = [123,25,67,25,137,225,24,467];
b.countingSort();
console.log(b);

Related

  1. alphanumSort( caseInsensitive )
    Array.prototype.alphanumSort = function( caseInsensitive ) {
      for ( var z = 0, t; t = this[z]; z++ ) {
        this[z] = [];
        var x = 0, y = -1, n = 0, i, j;
        while (i = ( j = t.charAt( x++ ) ).charCodeAt( 0 ) ) {
          var m = ( i == 46 || ( i >=48 && i <= 57 ) );
          if ( m !== n ) {
            this[z][++y] = "";
            n = m;
    ...
    
  2. alphanumSort(caseInsensitive)
    Array.prototype.alphanumSort = function(caseInsensitive) {
      for (var z = 0, t; t = this[z]; z++) {
        this[z] = [];
        var x = 0, y = -1, n = 0, i, j;
        while (i = (j = t.charAt(x++)).charCodeAt(0)) {
          var m = (i == 46 || (i >=48 && i <= 57));
          if (m !== n) {
            this[z][++y] = "";
            n = m;
    ...
    
  3. alphanumSort(caseInsensitive)
    Array.prototype.alphanumSort = function(caseInsensitive) {
      for (var z = 0, t; t = this[z]; z++) {
        this[z] = new Array();
        var x = 0, y = -1, n = 0, i, j;
        while (i = (j = t.charAt(x++)).charCodeAt(0)) {
          var m = (i == 46 || (i >=48 && i <= 57));
          if (m !== n) {
            this[z][++y] = "";
            n = m;
    ...
    
  4. ascendSortascendSort()
    'use strict'
    Array.prototype.ascendSort = function ascendSort() {
        return this.sort(function(a, b){
            return a > b ? 1 : a < b ? -1 : 0;
        });
    };
    
  5. asort(key)
    function xsort(sqs,col,order){
        sqs.sort(function(a,b){
            return((a[col]-b[col])*order);
        });
        return(sqs);
    Array.prototype.asort = function(key) {
        this.sort(function(a, b) {
            return (a[key] > b[key]) ? 1 : -1;
    ...
    
  6. descendSortascendSort()
    Array.prototype.descendSort = function ascendSort() {
        return this.sort(function(a, b){
            return a < b ? 1 : a > b ? -1 : 0;
        });
    };
    
  7. humanSort()
    Array.prototype.humanSort = function() {
      return this.sort(function(a, b) {
        aa = a.split(/(\d+)/);
        bb = b.split(/(\d+)/);
        for(var x = 0; x < Math.max(aa.length, bb.length); x++) {
          if(aa[x] != bb[x]) {
            var cmp1 = (isNaN(parseInt(aa[x],10)))? aa[x] : parseInt(aa[x],10);
            var cmp2 = (isNaN(parseInt(bb[x],10)))? bb[x] : parseInt(bb[x],10);
            if(cmp1 == undefined || cmp2 == undefined)
    ...
    
  8. keySort(key, desc)
    Array.prototype.keySort = function(key, desc){
      this.sort(function(a, b) {
        var result = desc ? (a[key] < b[key]) : (a[key] > b[key]);
        return result ? 1 : -1;
      });
      return this;
    
  9. listSort(sortby,direction)
    Array.prototype.listSort=function (sortby,direction){
             var objt=this;
             direction=direction.toUpperCase( );
             var obj =objt.slice(0); 
         obj = obj.sort(function (a,b){
         if(sortby.indexOf(".")<0){
           var codeA=a[sortby]==null?0:a[sortby];
           var codeB=b[sortby]==null?0:b[sortby];
         else{
           var codeA=eval("a."+sortby)==null?0:eval("a."+sortby);
           var codeB=eval("b."+sortby)==null?0:eval("b."+sortby);
          if(direction==="DESC"||direction==="ASC"||direction===" "){
            if(direction==="DESC"){
              t=codeA;codeA=codeB;codeB=t;
            if (typeof codeB=="string"){
              return codeB.localeCompare(codeA );
            else if(typeof codeB=="number"){
              return codeB>codeA;
          });
        return obj;
    };