Huffman’s algorithm. Builds Huffman trees, which can be used for building optimal prefix codes, for example.
Initially, each element (for example, character in an alphabet) is made into a single-node tree, with a weight equal to
its frequency. In each iteration, the two lightest trees are picked, combining them with a new root and giving the new
tree a weight equal to the sum of the original two tree weights. This can be done in loglinear time (or, in fact, in linear
time if the frequencies are presorted). (See Listing 7-1.)
Insertion sort. A simple sorting algorithm with quadratic running time. It works by repeatedly inserting the next
unsorted element in an initial sorted segment of the array. For small data sets, it can actually be preferable to more
advanced (and optimal) algorithms such as merge sort or quicksort. (In Python, though, you should use list.sort or
sorted if at all possible.) (See Listing 4-3.)
Do'stlaringiz bilan baham: |