Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the Damerau-Levenshtein Distance sort. #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ http://www.nczonline.net/blog/2009/04/13/computer-science-in-javascript-linked-l
Selection Sort
http://www.nczonline.net/blog/2009/09/08/computer-science-in-javascript-selection-sort/

Damerau-Levenshtein Distance Sort
http://en.wikipedia.org/wiki/Damerau–Levenshtein_distance

Please note: Since this is the repository that goes along with my blog post series, only pull requests for bugs are accepted.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Damerau-Levenshtein sort implementation in JavaScript
* Copyright (c) 2012 Nicholas C. Zakas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of items 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 items 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.
*/

/**
* A functional Damerau-Levenshtein sort implementation in JavaScript.
* @param {Array} x: the first array
* @param {Array} y: the second array
* @return {Array} the distance to sort by
*/

var damerauLevenshteinSort = function(x, y) {
var xLength = x.length, yLength = y.length;

if (xLength === 0) return yLength;
if (yLength === 0) return xLength;
if (xLength == yLength) return 0;

var matrix = new Array(xLength + 1), i = xLength, j = yLength;
for (; i >= 0; i--) matrix[i] = [i];
for (; j >= 0; j--) matrix[0][j] = j;

// Calculate matrix.
var this_i, that_j, cost, min, t, x, y;
for (i = 1, x = 0; i <= xLength; ++i, ++x) {
// x = i - 1;
this_i = __this[x];

// Step 4
for (j = 1, y = 0; j <= yLength; ++j, ++y) {
// Check the jagged ld total so far
if (i === j && matrix[i][j] > 4) return xLength;

// y = j - 1;
that_j = that[y];
cost = (this_i === that_j) ? 0 : 1; // Step 5
// Calculate the minimum (much faster than Math.min(...)).
min = matrix[x][j] + 1; // Deletion.
if ((t = matrix[i][y] + 1 ) < min) min = t; // Insertion.
if ((t = matrix[x][y] + cost) < min) min = t; // Substitution.

matrix[i][j] = min; // Update matrix.
}
}

return matrix[xLength][yLength];
};