Created
September 15, 2015 19:10
-
-
Save nailkhasipov/6b36c0de2eefe081e20b to your computer and use it in GitHub Desktop.
Quick-find implementation of Princeton Union-Find algorithm on JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
var id_array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
var union = function (p, q) { | |
var pId = id_array[p]; | |
var qId = id_array[q]; | |
if (items[p] === items[q]) { | |
return; | |
} | |
for (i = 0; i < items.length; ++i){ | |
if (id_array[i] === pId){ | |
id_array[i] = qId; | |
} | |
} | |
console.log(id_array); | |
}; | |
console.log(id_array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment