sortme
Version:
Simple sorting library written in ES6
1 lines • 3.8 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.sortme=e():t.sortme=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(1);Object.defineProperty(e,"MergeSort",{enumerable:!0,get:function(){return n.MergeSort}})},function(t,e,r){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0}),e.MergeSort=void 0;var a=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),u=r(2);e.MergeSort=function(t){function e(t,r){return n(this,e),o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,r))}return i(e,t),a(e,[{key:"_merge",value:function(t,e,r){for(var n=[],o=[],i=this.data,a=t;a<=e;a++){var u=i[a];n.push(u)}for(var a=e+1;a<=r;a++){var u=i[a];o.push(u)}for(var f=t,s=0,c=0;s<n.length&&c<o.length;){if(null!=this._config.key)var l=n[s][this._config.key],h=o[c][this._config.key];else var l=n[s],h=o[c];l<=h?(i[f]=n[s],s++):(i[f]=o[c],c++),f++}for(;s<n.length;)i[f]=n[s],s++,f++;for(;c>o.length;)i[f]=o[c],c++,f++}},{key:"_mergeSort",value:function(t,e){if(e>t){var r=Math.floor((t+e)/2);this._mergeSort(t,r),this._mergeSort(r+1,e),this._merge(t,r,e)}}},{key:"sort",value:function(){this._mergeSort(0,this.data.length-1),this.isSorted=!0}}]),e}(u.Sort)},function(t,e){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}();e.Sort=function(){function t(e,n){r(this,t),this.data=e,this.isSorted=!1,this._config={autorun:!0,watch:!1,key:null,copy:!1},n&&Object.assign(this._config,n),this._config.autorun&&this.sort()}return n(t,[{key:"sort",value:function(){}},{key:"swap",value:function(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}},{key:"reverse",value:function(){for(var t=Math.floor(this.data.length/2),e=0;e<t;e++)this.swap(this.data,e,this.data.length-1-e)}},{key:"_find",value:function(t,e,r){var n=Math.floor((e+r)/2);if(this._config.key)var o=this.data[n][this._config.key];else var o=this.data[n];return e>=r?null:t<o?this._find(t,e,n):t>o?this._find(t,n+1,r):o===t?n:void 0}},{key:"find",value:function(t){return this._find(t,0,this.data.length)}},{key:"findAll",value:function(t){var e=this._find(t,0,this.data.length);if(null===e)return null;for(var r=[],n=e-1;n>=0&&this.data[n]===t;n--)r.push(n);r.push(e);for(var n=e+1;n<this.data.length&&this.data[n]===t;n++)r.push(n);return r}},{key:"getArr",value:function(){return this.data}},{key:"printArr",value:function(){console.log(this.data)}}]),t}()}])});