Javascript Binary Search / Insertion Preformance
function binarySearch(value)
{
var startIndex = 0,
stopIndex = words.length - 1,
middle = Math.floor((stopIndex + startIndex) / 2);
while (words[middle] != value && startIndex < stopIndex) {
// adjust search area
if (value < words[middle]) {
stopIndex = middle - 1;
} else if (value > words[middle]) {
startIndex = middle + 1;
}
// recalculate middle
middle = Math.floor((stopIndex + startIndex) / 2);
}
}
Ich mache eine große Liste von Wörtern im Format eines Arrays:
z.B.["a","ab","abc","b"]
In alphabetischer Reihenfolge. Das Problem, das ich habe, ändert meinen binären Suchalgorithmus, um das Wort an der richtigen Stelle hinzuzufügen und dann zu aktualisieren?
Was ist der beste Weg, um ein Wort in ein geordnetes Array einzufügen? Und warum ist es der beste Weg, dies zu tun?