Permalink
Browse files

mod: reverse the order of `array` and `n` for nlargest and nsmallest

note: this breaks backward compatibility
  • Loading branch information...
1 parent a3ca8c5 commit 979a1f49af0cedb17604e2143a2391ae40aa871d @qiao committed Apr 22, 2012
Showing with 13 additions and 13 deletions.
  1. +4 −4 README.md
  2. +2 −2 lib/heap.js
  3. +2 −2 src/heap.coffee
  4. +5 −5 test/heap.test.coffee
View
@@ -57,8 +57,8 @@ find 3 largest/smallest items in an array
```js
var array = [1, 3, 4, 2, 5];
-Heap.nlargest(3, array); // [5, 4, 3]
-Heap.nsmallest(3, array); // [1, 2, 3]
+Heap.nlargest(array, 3); // [5, 4, 3]
+Heap.nsmallest(array, 3); // [1, 2, 3]
```
Document
@@ -173,11 +173,11 @@ Fast version of a heappush followed by a heappop.
Build the heap.
-**nlargest(n, array, [cmp])**
+**nlargest(array, n, [cmp])**
Find the n largest elements in a dataset.
-**nsmallest(n, array, [cmp])**
+**nsmallest(array, n, [cmp])**
Find the n smallest elements in a dataset.
View
@@ -125,7 +125,7 @@
Find the n largest elements in a dataset.
*/
- nlargest = function(n, array, cmp) {
+ nlargest = function(array, n, cmp) {
var elem, result, _i, _len, _ref;
if (cmp == null) cmp = defaultCmp;
result = array.slice(0, n);
@@ -143,7 +143,7 @@
Find the n smallest elements in a dataset.
*/
- nsmallest = function(n, array, cmp) {
+ nsmallest = function(array, n, cmp) {
var elem, i, los, result, _i, _len, _ref, _ref2, _results;
if (cmp == null) cmp = defaultCmp;
if (n * 10 <= array.length) {
View
@@ -82,7 +82,7 @@ heapify = (array, cmp=defaultCmp) ->
###
Find the n largest elements in a dataset.
###
-nlargest = (n, array, cmp=defaultCmp) ->
+nlargest = (array, n, cmp=defaultCmp) ->
result = array[0...n]
return result unless result.length
heapify(result, cmp)
@@ -92,7 +92,7 @@ nlargest = (n, array, cmp=defaultCmp) ->
###
Find the n smallest elements in a dataset.
###
-nsmallest = (n, array, cmp=defaultCmp) ->
+nsmallest = (array, n, cmp=defaultCmp) ->
if n * 10 <= array.length
result = array[0...n].sort(cmp)
return result unless result.length
View
@@ -59,18 +59,18 @@ describe 'Heap#clone', ->
describe 'Heap.nsmallest', ->
it 'should return exactly n elements when size() >= n', ->
- Heap.nsmallest(3, [1..10]).should.eql([1..3])
+ Heap.nsmallest([1..10], 3).should.eql([1..3])
array = [1,3,2,1,3,4,4,2,3,4,5,1,2,3,4,5,2,1,3,4,5,6,7,2]
- Heap.nsmallest(2, array).should.eql([1, 1])
+ Heap.nsmallest(array, 2).should.eql([1, 1])
it 'should return size() elements when size() <= n', ->
- Heap.nsmallest(10, [3..1]).should.eql([1..3])
+ Heap.nsmallest([3..1], 10).should.eql([1..3])
describe 'Heap.nlargest', ->
it 'should return exactly n elements when size() >= n', ->
- Heap.nlargest(3, [1..10]).should.eql([10..8])
+ Heap.nlargest([1..10], 3).should.eql([10..8])
it 'should return size() elements when size() <= n', ->
- Heap.nlargest(10, [3..1]).should.eql([3..1])
+ Heap.nlargest([3..1], 10).should.eql([3..1])

0 comments on commit 979a1f4

Please sign in to comment.