Skip to content

Instantly share code, notes, and snippets.

@nailkhasipov
Created September 15, 2015 19:10
Show Gist options
  • Save nailkhasipov/6b36c0de2eefe081e20b to your computer and use it in GitHub Desktop.
Save nailkhasipov/6b36c0de2eefe081e20b to your computer and use it in GitHub Desktop.
Quick-find implementation of Princeton Union-Find algorithm on JavaScript
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