Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added license for the bubbleSort that N.C.Zakas created

  • Loading branch information...
commit d44512a87fe0c477059afac4dcf566cb43d8e718 1 parent d6b79a8
@3rd-Eden authored
Showing with 155 additions and 5 deletions.
  1. +136 −5 algorithms.js
  2. +19 −0 licenses/NCZAKAS
View
141 algorithms.js
@@ -1,5 +1,8 @@
-// see http://en.wikipedia.org/wiki/Soundex
-// Soundex allows you to check how close strings are matching.
+/**
+ * Calculates the soundex of a given string
+ * @param {String} string the String that needs to be converted to a soundex value.
+ * @return {String} the soundex of the string
+ */
exports.soundex = function( string ){
string = string.toUpperCase();
@@ -25,8 +28,14 @@ exports.soundex = function( string ){
return soundx.join( '' );
};
-// see http://en.wikipedia.org/wiki/Bisection_method
-// What it basically does is return the index where item X should be placed in the array, assuming the array is sorted
+/**
+ * Calculates the index of the Array where item X should be placed, assuming the Array is sorted.
+ * @param {Array} array The array containing the items.
+ * @param {int} x The item that needs to be added to the array.
+ * @param {int} low Inital Index that is used to start searching, optional.
+ * @param {int} high The maximum Index that is used to stop searching, optional.
+ * @return {int} the index where item X should be placed
+ */
exports.bisect = exports.bisectRight = function( array, x, low, high ){
// The low and high bounds the inital slice of the array that needs to be searched
@@ -48,7 +57,15 @@ exports.bisect = exports.bisectRight = function( array, x, low, high ){
return low;
};
-exports.bisectLeft = function( array, x, low, high ){
+/**
+ * Calculates the index of the Array where item X should be placed, assuming the Array is sorted.
+ * @param {Array} array The array containing the items.
+ * @param {int} x The item that needs to be added to the array.
+ * @param {int} low Inital Index that is used to start searching, optional.
+ * @param {int} high The maximum Index that is used to stop searching, optional.
+ * @return {int} the index where item X should be placed
+ */
+exports.bisectLeft = function( array, x, low , high ){
// The low and high bounds the inital slice of the array that needs to be searched
// this is optional
@@ -67,4 +84,118 @@ exports.bisectLeft = function( array, x, low, high ){
}
return low;
+};
+
+
+
+/**
+ * Creates array ranges or applies padding to an array.
+ * @param {Array} array The array that needs the padding.
+ * @param {int} size The amount of padding needs to be added to the array.
+ * @param {int} value The value that is used for the padding, optional. If omitted the padding index is used.
+ * @return {Array} The array with padding applied.
+ */
+var range = function( array, size, value ){
+ var length = Math.abs( size ) - array.length, i = 0;
+ if( length <= 0 )
+ return array;
+
+ for(; i <= length; i++ )
+ size < 0 ? array.unshift( value || i ) : array[ array.length ] = value || i;
+
+ return array;
+};
+
+/**
+ * Calculates the Levenshtein distance between a and b.
+ * @param {String} a Item 1.
+ * @param {String} b Item 2.
+ * @return {int} the distance.
+ */
+exports.levenshtein = function( a, b ){
+ if( a === b ) return 0;
+
+ var a_len = a.length, b_len = b.length, tmp;
+
+ if( a_len === 0 ) return b_len;
+ if( b_len === 0 ) return a_len;
+
+ // make sure a_len <= b_len
+ if( a_len > b_len ){
+ tmp = a_len;
+ a_len = b_len; b_len = tmp;
+
+ tmp = a;
+ a = b; b = tmp;
+ }
+
+ var current = range( [], a_len + 1),
+ b_loop = b_len + 1,
+ a_loop, previous, adds, changes, deletes, min,
+ i = 1,j;
+
+ for(; i < b_loop; i++ ){
+ previous = current;
+ current = range( [i], a_len, 0 );
+
+ for( j = 1, a_loop = a_len + 1; j < a_loop; j++ ){
+ adds = previous[ j ] + 1;
+ deletes = current[ j-1 ] + 1;
+ changes = previous[ j-1 ];
+
+ if( a[j-1] != b[i-1] )
+ changes = changes + 1;
+
+ if( adds < deletes && adds < changes )
+ min = adds;
+ else if( deletes < adds && deletes < changes )
+ min = deletes;
+ else
+ min = changes;
+
+ current[ j ] = min;
+ }
+ }
+
+ return current[ a_len ]
+
+};
+
+/*
+ * The following functions are Copyright (c) 2009 Nicholas C. Zakas
+ * LICENSED: http://github.com/3rd-Eden/node-algorithms/licenses/NCZAKAS
+ *
+ * bubbleSort: http://github.com/nzakas/computer-science-in-javascript/blob/master/algorithms/sorting/bubble-sort/bubble-sort-2.js
+ */
+
+/**
+ * Swaps two values in an array.
+ * @param {Array} items The array containing the items.
+ * @param {int} firstIndex Index of first item to swap.
+ * @param {int} secondIndex Index of second item to swap.
+ * @return {void}
+ */
+var swap = function(items, firstIndex, secondIndex ){
+ var temp = items[ firstIndex ];
+ items[ firstIndex ] = items[ secondIndex ];
+ items[ secondIndex ] = temp;
+};
+
+/**
+ * A bubble sort implementation in JavaScript. The array
+ * is sorted in-place. This uses two reversed loops that
+ * count down instead of counting up.
+ * @param {Array} items An array of items to sort.
+ * @return {Array} The sorted array.
+ */
+exports.bubbleSort = function(items){
+ var len = items.length,
+ i = len-1, j;
+
+ for( ; i >= 0; i-- )
+ for( j=len-i; j >= 0; j-- )
+ if( items[j] < items[j-1] )
+ swap(items, j, j-1);
+
+ return items;
};
View
19 licenses/NCZAKAS
@@ -0,0 +1,19 @@
+Copyright (c) 2009 Nicholas C. Zakas
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
Please sign in to comment.
Something went wrong with that request. Please try again.