Nonlinear General Selection Algorithm
Using the same ideas used in minimum/maximum algorithms, we can construct a simple, but inefficient general algorithm for finding the kth smallest or kth largest item in a list, requiring O(kn) time, which is effective when k is small. To accomplish this, we simply find the most extreme value and move it to the beginning until we reach our desired index. This can be seen as an incomplete selection sort. Here is the minimum-based algorithm:
function select(list, k) for i from 1 to k minIndex = i minValue = list for j from i+1 to n if list < minValue minIndex = j minValue = list swap list and list return listOther advantages of this method are:
- After locating the jth smallest element, it requires only O(j + (k-j)2) time to find the kth smallest element, or only O(1) for k ≤ j.
- It can be done with linked list data structures, whereas the one based on partition requires random access.
Read more about this topic: Selection Algorithm
Famous quotes containing the words general and/or selection:
“... women can never do efficient and general service in hospitals until their dress is prescribed by laws inexorable as those of the Medes and Persians. Then, that dress should be entirely destitute of steel, starch, whale-bone, flounces, and ornaments of all descriptions; should rest on the shoulders, have a skirt from the waist to the ankle, and a waist which leaves room for breathing.”
—Jane Grey Swisshelm (18151884)
“Historians will have to face the fact that natural selection determined the evolution of cultures in the same manner as it did that of species.”
—Konrad Lorenz (19031989)