Natural selection can be expressed as the following general algorithm:
Note that the above algorithm is made with no explicit reference to biological entities. Thus, a form of natural selection could occur in the non-biological realm. Note also that this formulation does not rule out selection occurring at all biological levels (e.g. gene, organism, group).
Darwin first outlined his theory in two unpublished manuscripts written in 1842 and 1844 and more fully developed it for publication in The Origin of Species, especially Chapter 4.
See also; [artifical selection]?, sexual selection, evolution