/** * * Sort.js * @version: 0.4.0 * * Sorting Algorithms implemented in JavaScript * https://github.com/foo123/SortingAlgorithms * **/!function(e,r,t){"use strict";"object"==typeof module&&module.exports?(module.$deps=module.$deps||{})&&(module.exports=module.$deps[r]=t.call(e)):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(r)?define(r,["module"],function(r){return t.moduleUri=r.uri,t.call(e)}):r in e||(e[r]=t.call(e)||1)&&"function"==typeof define&&define.amd&&define(function(){return e[r]})}(this,"Sort",function(){"use strict";function e(e,r){return E((r-e)*M()+e)}function r(e,r){return e=null==e?0:+e,r=null==r?1:+r,(r-e)*M()+e}function t(e,n){e=null==e?0:+e,n=null==n?1:+n;var i,o,a,s,u,l=!0;if(null!==t.spare)return o=e+n*t.spare,t.spare=null,o;for(;l;)i=r(-1,1),o=r(-1,1),a=i*i+o*o,l=0>=a||a>=1;return s=T(-2*x(a)/a),t.spare=o*s,u=i*s,e+n*u}function n(e,r){return e-r}function i(e,r){var t,n=new Array(e);for(r=r||0,t=0;e>t;t++)n[t]=r;return n}function o(e,r,t){var n,i,o=r,a=new Array(e);for(n=0,i=0;e>n;n++,i++)a[n]=o,i>=t&&(i=0,o+=r);return a}function a(e,r){var t,n=new Array(e);if(!0===r)for(t=0;e>t;t++)n[t]=e-1-t;else for(t=0;e>t;t++)n[t]=t;return n}function s(e,r,t,n){var i,o=new Array(e);for(n=n||(t-r)/e,o[0]=r,i=1;e>i;i++)o[i]=o[i-1]+n;return o}function u(r,t,n){var i,o=new Array(r);for(i=0;r>i;i++)o[i]=e(t,n);return o}function l(e,t,n){var i,o=new Array(e);for(i=0;e>i;i++)o[i]=r(t,n);return o}function f(r,t,n,i,o){var a,s,u=0,l=new Array(r);for(a=0,s=0;r>a;a++,s++)l[a]=e(t,n)+u,s>=o&&(s=0,u+=i);return l}function c(e,t,n,i,o){var a,s,u=0,l=new Array(e);for(a=0,s=0;e>a;a++,s++)l[a]=r(t,n)+u,s>=o&&(s=0,u+=i);return l}function h(r){for(var t,n,i=r.length;i--;)t=e(0,i),n=r[i],r[i]=r[t],r[t]=n;return r}function d(e){return e instanceof Array||"[object Array]"===k.call(e)}function g(e,r,t){var n,i,o=e.length;if(1>=o)return!0;if(!0===r)if(!0===t)for(i=e[0],n=1;o>n;n++){if(i<=e[n])return!1;i=e[n]}else for(i=e[0],n=1;o>n;n++){if(in;n++){if(i>=e[n])return!1;i=e[n]}else for(i=e[0],n=1;o>n;n++){if(i>e[n])return!1;i=e[n]}return!0}function m(e,r,t,n,i,o,a){var s=this;s.key=e,s.val=r,s.prev=t||null,s.next=n||null,s.left=i||null,s.right=o||null,s.data=a||null}function p(e,r,t){if(null!=r)for(var n,i,o,a,s=0,u=e.length;u>s;){if(n=e[s],s+=1,a=null,N===n)a=r;else if(O===n&&null!=r.prev)a=r.prev;else if(B===n&&null!=r.left)a=r.left;else if(R===n&&null!=r.right)a=r.right;else{if(q!==n||null==r.next)continue;a=r.next}if(r===a)t(a);else if(d(a))for(i=0,o=a.length;o>i;i++)p(e,a[i],t);else p(e,a,t)}}var y={VERSION:"0.4.0"},b=(Function.prototype,Object.prototype),v=Array.prototype,w=v.slice,k=b.toString,S=Math,A=S.min,I=S.max,T=S.sqrt,x=S.log,M=S.random,E=S.round,N=1,O=2,q=3,B=4,R=5;t.spare=null,m.NODE=N,m.PREV=O,m.NEXT=q,m.LEFT=B,m.RIGHT=R,m.walk=p,y.Node=m,y.Array=Array,y.Array32F="undefined"!=typeof Float32Array?Float32Array:Array,y.Array64F="undefined"!=typeof Float64Array?Float64Array:Array,y.Array8I="undefined"!=typeof Int8Array?Int8Array:Array,y.Array16I="undefined"!=typeof Int16Array?Int16Array:Array,y.Array32I="undefined"!=typeof Int32Array?Int32Array:Array,y.Array8U="undefined"!=typeof Uint8Array?Uint8Array:Array,y.Array16U="undefined"!=typeof Uint16Array?Uint16Array:Array,y.Array32U="undefined"!=typeof Uint32Array?Uint32Array:Array,y.Shuffle=h,y.isSorted=g,y.utils={RandomInteger:e,Uniform:r,Normal:t,asNumbers:n,Constant:i,Duplicates:o,Equidistant:s,IntegerEquidistributable:u,NumberEquidistributable:l,IntegerDynamicRange:f,NumberDynamicRange:c,Range:a};var C=y.utils.Timer=function(e){var r=arguments.length?e:(new Date).getTime(),t=1/0,n=1/0,i=0,o=this;o.getMs=function(){var e=t-r;return n=A(n,e),i=I(i,e),e},o.start=function(){return r=(new Date).getTime(),o},o.end=function(){return t=(new Date).getTime(),o},o.reset=function(){return r=(new Date).getTime(),t=1/0,o},o.update=function(){return t=(new Date).getTime(),r=t,o}};return y.Time=function(e,r){var t,n,i=200,o=200,a=i+o;return r&&(n=w.call(arguments,2),t=new C,t.start(),setTimeout(function(){r.apply(null,n),setTimeout(function(){t.end();var r=t.getMs()-a;e&&e.call(t,r)},o)},i)),0},y.vTime=function(e,r,t){return t=t||[],y.Time.apply(y,[e,r].concat(t))},!function(e,r){var t=e.utils.asNumbers;e.BuiltinSort=function(e){return e.sort(t),e},e.BuiltinSort.reference="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort",e.BuiltinSort.description="This is JavaScript's builtin algorithm, usually a variation of quicksort."}(y),!function(e,r){e.BubbleSort=function(e){var r,t,n,i=e.length;if(1>=i)return e;do{for(t=0,r=1;i-1>=r;r++)e[r-1]>e[r]&&(n=e[r-1],e[r-1]=e[r],e[r]=n,t=r);i=t}while(i>0);return e},e.BubbleSort.reference="http://en.wikipedia.org/wiki/Bubble_sort",e.BubbleSort.description="Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted."}(y),!function(e,r){e.CocktailSort=function(e){var r,t,n,i,o,a=e.length;if(1>=a)return e;r=-1,t=a-2;do{for(n=!1,r++,i=r;t>=i;i++)e[i]>e[i+1]&&(o=e[i],e[i]=e[i+1],e[i+1]=o,n=!0);if(!1===n)break;for(n=!1,t--,i=t;i>=r;i--)e[i]>e[i+1]&&(o=e[i],e[i]=e[i+1],e[i+1]=o,n=!0)}while(n);return e},e.CocktailSort.reference="http://en.wikipedia.org/wiki/Cocktail_shaker_sort",e.CocktailSort.description="Cocktail sort is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts."}(y),!function(e,r){var t=[701,301,132,57,23,10,4,1],n=t.length;e.ShellSort=function(e){var r,i,o,a,s,u=e.length;if(1>=u)return e;for(o=0;n>o;o++)for(r=t[o],a=r;u>a;a+=r){for(i=e[a],s=a;s>=r&&e[s-r]>i;s-=r)e[s]=e[s-r];e[s]=i}return e},e.ShellSort.reference="http://en.wikipedia.org/wiki/Shellsort",e.ShellSort.description="Shellsort is an in-place comparison sort. It generalizes an exchanging sort, such as insertion or bubble sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighboring elements. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange."}(y),!function(e,r){var t=Math.floor,n=function(e,r,t){var i,o=2*r+1,a=2*r+2,s=r;t>o&&e[o]>e[r]&&(s=o),t>a&&e[a]>e[s]&&(s=a),s!==r&&(i=e[r],e[r]=e[s],e[s]=i,n(e,s,t))},i=function(e){var r,i=e.length;for(r=t(i/2);r>=0;r--)n(e,r,i);return e};e.HeapSort=function(e){var r,t,o=e.length;if(1>=o)return e;for(i(e),t=o-1;t>0;t--)r=e[0],e[0]=e[t],e[t]=r,o--,n(e,0,o);return e},e.HeapSort.reference="http://en.wikipedia.org/wiki/Heap_sort",e.HeapSort.description="Heapsort is a comparison-based sorting algorithm to create a sorted array (or list), and is part of the selection sort family. Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more favorable worst-case O(nlogn) runtime."}(y),!function(e,r){e.InsertionSort=function(e){var r,t,n,i=e.length;if(1>=i)return e;for(r=1;i>r;r++){for(t=e[r],n=r;n>0&&t=a)return e;for(r=0;a>r;r++){for(t=e[r],n=r,i=r+1;a>i;i++)e[i]i;i++)e[i]o&&null==e[o];)o++;if(o!==n)if(o!==t){if(e[o]===r)return o+1;e[o]>r?n=o-1:t=o+1}else{if(e[o]>r)return o;t=o+1}else{if(null!=e[o]&&e[o]<=r)return o+1;n=i}}}function n(e,r,t,n){var i;if(null==e[t])e[t]=r;else{for(;null!=e[t];)i=e[t],e[t]=r,r=i,t++;e[t]=r,t++}t>n[0]&&(n[0]=t)}function i(e,r,t,n){var i,o,a,s,u,l,f=e.length;for(i=new Array(f),o=a=1,s=u=0;t>o;){for(l=0;r>l;)null!=e[a]&&(i[u]=e[a],u++),e[a]=null,a++,l++;null!=e[a]&&(i[u]=e[a],u++),e[a]=i[s],a++,s++,o++}n[0]=a-1}e.LibrarySort=function(e,r){var o,a,s,u,l,f,c,h,d,g,m=e.length;for(r=r||0,s=~~(r*m+.5),a=m+s,o=new Array(a),d=0;a>d;d++)o[d]=null;for(o[0]=e[0],u=[0],l=f=1;m>l;){for(c=l;m>l&&c>0;)h=t(o,e[f],0,u[0]),n(o,e[f],h,u),c--,l++,f++;i(o,s,l,u)}for(g=0,d=0;a>d;d++)null!=o[d]&&(e[g++]=o[d]);return e},e.LibrarySort.reference="http://en.wikipedia.org/wiki/Library_sort",e.LibrarySort.description="Library sort, or gapped insertion sort is a sorting algorithm that uses an insertion sort, but with gaps in the array to accelerate subsequent insertions. Like the insertion sort it is based on, library sort is a stable comparison sort and can be run as an online algorithm; however, it was shown to have a high probability of running in O(nlogn) time (comparable to quicksort), rather than an insertion sort's O(n2)."}(y),!function(e,r){function t(e,r,t,n,i,o,a,s){if(t>=o)return r;a=a||1,s=s||1;for(var u=o-t+1,l=0,f=0>a?n:t,c=0>s?o:i,h=0>a?t:n,d=0>s?i:o;a*(h-f)>=0&&s*(d-c)>=0;)r[f]r[c]?(e[l++]=r[c],c+=s):(e[l++]=r[f],e[l++]=r[c],f+=a,c+=s);for(;a*(h-f)>=0;)e[l++]=r[f],f+=a;for(;s*(d-c)>=0;)e[l++]=r[c],c+=s;for(l=0;u>l;l++)r[t+l]=e[l];return r}function n(e,r,t,n){var i,o,a,s,u,l,f,c,h;for(n[0]=-1,n[1]=-1,n[2]=0,n[3]=-1,n[4]=-1,n[5]=0,l=0,h=0,s=r,u=-1,i=e[s],a=s+1;t>=a;a++){if(o=e[a],o>i){if(-1===l){u=a-1;break}0===l&&(l=1)}else if(i>o){if(1===l){u=a-1;break}0===l&&(l=-1)}i=o}if(0===l&&(l=1),-1===u)u=t,n[0]=s,n[1]=u,n[2]=l;else{for(f=u+1,c=-1,i=e[f],a=f+1;t>=a;a++){if(o=e[a],o>i){if(-1===h){c=a-1;break}0===h&&(h=1)}else if(i>o){if(1===h){c=a-1;break}0===h&&(h=-1)}i=o}-1===c&&(c=t),0===h&&(h=1),n[0]=s,n[1]=u,n[2]=l,n[3]=f,n[4]=c,n[5]=h}}function i(e,r,n,o){if(r>=n)return e;null==o&&(o=new Array(e.length));var a=r+(n-r+1>>>1);return i(e,r,a-1,o),i(e,a,n,o),t(o,e,r,a-1,a,n),e}function o(e){var r=e.length;if(1>=r)return e;for(var n,i,o=r,a=1,s=2,u=Math.min,l=new Array(r);o;){for(i=r-a,n=0;i>n;n+=s)t(l,e,n,n+a-1,n+a,u(n+s-1,r-1));a<<=1,s<<=1,o>>=1}return e}function a(e){var r=e.length;if(1>=r)return e;var i,o,a,s=new Array(r),u=[-1,-1,0,-1,-1,0],l=0,f=r-1,c=l,h=-1;do{if(n(e,l,f,u),-1===u[3]){if(-1===u[2]&&f>l)for(i=l,o=f;o>i;i++,o--)a=e[i],e[i]=e[o],e[o]=a;i=l,o=f}else t(s,e,u[0],u[1],u[3],u[4],0>u[2]?-1:1,0>u[5]?-1:1),i=u[0],o=u[4];-1!==h&&t(s,e,c,h,i,o),h=o,l=o+1}while(f>=l);return e}e.RecursiveMergeSort=function(e){return i(e,0,e.length-1)},e.MergeSort=function(e){return o(e)},e.NaturalMergeSort=function(e){return a(e)},e.MergeSort.Merge=t,e.NaturalMergeSort.Run=n,e.RecursiveMergeSort.reference=e.NaturalMergeSort.reference=e.MergeSort.reference="http://en.wikipedia.org/wiki/Merge_sort",e.RecursiveMergeSort.description=e.NaturalMergeSort.description=e.MergeSort.description="Merge sort is an O(nlogn) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm. A natural merge sort is similar to a bottom up (iterative) merge sort except that any naturally occurring runs (sorted sequences) in the input are exploited. In the typical case, the natural merge sort may not need as many passes because there are fewer runs to merge. In the best case, the input is already sorted (i.e., is one run), so the natural merge sort need only make one pass through the data. In many practical cases, long natural runs are present. Natural merge sort has linear O(n) performance on average and O(nlogn) in worst case (similar to merge sort)."}(y),!function(e,r){function t(e,r,t){var n,i,o,a,s;for(n=~~(r+.5*(t-r)),i=e[n],o=e[n],e[n]=e[t],e[t]=o,a=r,s=r;t>s;s++)e[s]<=i&&(o=e[s],e[s]=e[a],e[a]=o,a++);return o=e[a],e[a]=e[t],e[t]=o,a}function n(e,r,i){if(r>=i)return e;var o;return o=t(e,r,i),n(e,r,o-1),n(e,o+1,i),e}function i(e,r,n){if(r>=n)return e;var i,o,a,s;for(a=n-r+1,i=new Array(a),o=-1,i[++o]=r,i[++o]=n;o>=0;)n=i[o--],r=i[o--],s=t(e,r,n),s-1>r&&(i[++o]=r,i[++o]=s-1),n>s+1&&(i[++o]=s+1,i[++o]=n);return e}e.RecursiveQuickSort=function(e){return n(e,0,e.length-1)},e.QuickSort=function(e){return i(e,0,e.length-1)},e.QuickSort.Partition=t,e.RecursiveQuickSort.reference=e.QuickSort.reference="http://en.wikipedia.org/wiki/Quicksort",e.RecursiveQuickSort.description=e.QuickSort.description="Quicksort is a sorting algorithm that, on average, makes O(nlogn) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare. Quicksort is often faster in practice than other O(nlogn) algorithms. Additionally, quicksort's sequential and localized memory references work well with a cache. Quicksort is a comparison sort and, in efficient implementations, is not a stable sort."}(y),!function(e,r){var t=e.Node;e.TreeSort=function(e){var r,n,i,o,a,s=e.length;if(1>=s)return e;for(i=new t(0,e[0]),o=0,s>=2&&(o++,a=e[o],ao+1;){for(n=i,o++,a=e[o],r=n;n;)r=n,n=a=f)return e;for(r=t=e[0],a=1;f>a;a++)e[a]>t?t=e[a]:e[a]a;a++)n[a]=0;for(a=0;f>a;a++)n[~~e[a]]++;for(o=0,a=0;s>a;a++)i=n[a],n[a]=o,o+=i;for(l=e.slice(),a=0;f>a;a++)u=l[a],e[n[~~u]]=u,n[~~u]++;return e},e.CountingSort.reference="http://en.wikipedia.org/wiki/Counting_sort",e.CountingSort.description="Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers, that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently."}(y),!function(e,r){function t(e){var r,t,n,i=e.length;for(n=1;i>n;n++){for(r=e[n],t=n-1;t>=0&&rn;n++)t=e[n],r=a(t/s),i[r]||(i[r]=[]),i[r].push(t);return i}function i(e,r){var t,n=e.length;for(t=0;n>t;t++)e[t]&&(e[t]=r(e[t]));return e}function o(e){var r,t,n=[],i=e.length;for(r=0;i>r;r++)t=e[r],t&&(n=n.concat(t));return n}var a=Math.floor,s=t;e.BucketSort=function(e,r){var t,a,u,l,f=e.length;if(1>=f)return e;for(s=r||s,a=n(e),i(a,s),t=o(a),l=1&f,u=0;f>u;u+=2)e[u]=t[u],e[u+1]=t[u+1];return l&&(e[f-1]=t[f-1]),e},e.BucketSort.reference="http://en.wikipedia.org/wiki/Bucket_sort",e.BucketSort.description="Bucket sort is a sorting algorithm that works by partitioning an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. Bucket sort is a generalization of pigeonhole sort. Since bucket sort is not a comparison sort, the Ω(nlogn) lower bound is inapplicable. The computational complexity estimates involve the number of buckets."}(y),!function(e,r){e.RadixSort=function(e){return e},e.RadixSort.reference="http://en.wikipedia.org/wiki/Radix_sort",e.RadixSort.description="NOT IMPLEMENTED YET In computer science, radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. Radix sort efficiency is O(d n) for n keys which have d or fewer digits. Sometimes d is presented as a constant, which would make radix sort better (for sufficiently large n) than the best comparison-based sorting algorithms, which are all O(n log(n)) number of comparisons needed. However, in general d cannot be considered a constant."}(y),!function(e,r){var t=e.Array,n=Math.round;Math.floor;e.PermutationSort=function(e){var r,t,n,i=e.length;if(1>=i)return e;for(r=e.slice(),t=0;i>t;t++)n=r[t],e[n]=n;return e},e.PermutationSort.reference="#A Custom Algorithm",e.PermutationSort.description="Algorithm for sorting permutations of the integer set [0, N-1], with no duplicates, by (trivial) re-indexing",e.IndexSort=function(e){var r,i,o,a,s,u,l,f,c,h,d,g,m,p,y,b,v=e.length;if(1>=v)return e;for(r=i=e[0],b=e[1]-e[0],m=b?0>b?-1:1:0,p=!m,y=!0,s=1;v>s;s++)y&&(b=e[s]-e[s-1],g=b?0>b?-1:1:0,p&&g&&(m=g,p=!1),g&&g-m&&(y=!1)),e[s]>i?i=e[s]:e[s]g)for(s=0;v>s;s++)b=e[s],e[s]=e[v-1-s],e[v-1-s]=b;return e}for(o=(v-1)/(i-r),f=v,c=new t(v),l=new t(f),s=0;f>s;s++)l[s]=0;for(s=0;v>s;s++)b=e[s],u=c[s]=n(o*(b-r)),l[u]++;for(h=0,s=0;f>s;s++)d=l[s],l[s]=h,h+=d;for(a=e.slice(),s=0;v>s;s++)u=c[s],e[l[u]]=a[s],l[u]++;return e},e.IndexSort.reference="#A Custom Algorithm",e.IndexSort.description="Algorithm for sorting random numbers close to being 'homogeneous-equidistant' in [m, M], with possible duplicates, by re-indexing (an extended version of PermutationSort)"}(y),!function(e,r){e.TimSort=function(e){return e},e.TimSort.reference="http://en.wikipedia.org/wiki/Timsort",e.TimSort.description="NOT IMPLEMENTED YET Timsort is a hybrid sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It is used in the Python programming language. The algorithm finds subsets of the data that are already ordered, and uses that knowledge to sort the remainder more efficiently. This is done by merging an identified subset, called a run, with existing runs until certain criteria are fulfilled. Timsort has been Python's standard sorting algorithm since version 2.3. It is used to sort arrays in Java SE 7, on the Android platform, and in GNU Octave."}(y),!function(e,r){e.BurstSort=function(e){return e},e.BurstSort.reference="http://en.wikipedia.org/wiki/Burstsort",e.BurstSort.description="NOT IMPLEMENTED YET Burstsort and its variants are cache-efficient algorithms for sorting strings and are faster than quicksort for large data sets, first published in 2003. Burstsort algorithms use a trie to store prefixes of strings, with growable arrays of pointers as end nodes containing sorted, unique, suffixes (referred to as buckets)."}(y),y});