Complex systems follow simple rules to reach more optimal or stable states. This is referred to as self-organisation.

Shoaling and schooling behaviour is an example of self-organisation arising from local interactions between parts of an initially disordered system. The seemingly complex behaviour of fish schooling can occur when each individual fish follows three simple rules:

  1. Swim close to your neighbours, but
  2. Avoid crowding them, and
  3. Swim in the same direction they are.

You can find out more here.