Chaotic mobs of jackdaws suddenly get organised once enough birds join in, new research shows.
The birds form mobs to drive away predators near their nests, and are initially disordered.
But the new study, by biologists at the University of Exeter, physicists at Stanford University and computer scientists from Simon Fraser University in Canada, shows a dramatic switch to "ordered motion" once the group reaches a certain density.
The study also reveals that jackdaws follow different rules when mobbing predators than when flying to winter roosts.
"Traditionally, it is thought that flocking - and other collective behaviour like swarms, fish schools and human crowds - occurs when each individual follows an identical set of rules to every other in the group," said Dr Alex Thornton, of the Centre for Ecology and Conservation on Exeter's Penryn Campus in Cornwall.
"This study shows that these rules are actually flexible.
"When flying to their winter roosts, jackdaws follow what we call 'topological rules' - meaning that they respond to the movements of a fixed number of neighbours, and groups remain ordered regardless of how many birds are flying together.
"When mobbing a predator, however, the jackdaws instead use 'metric rules' where they respond to all the neighbours that are within a given distance. Here, we see order emerging from chaos.
"Jackdaws issue an alarm call to draw other birds to the mob, and at first these groups are completely disordered.
"Then, when the density of birds reaches a certain threshold, it suddenly flips into an ordered, cohesive state where the birds are aligned with their neighbours and move together in an organised way.
"This sudden transition from disorder to order is similar to the phase transitions we see in physics, like when water turns to ice."
This finding that rules of collective behaviour can change in different contexts could have implications for the design of autonomous vehicles, Dr Thornton said.
"Swarms of unmanned aerial vehicles or drones have potential uses in many applications including ecological surveys, fire-fighting and search-and-rescue in remote regions," he said.
"By learning from the behaviour of jackdaws, we can begin to design drones that modify their swarming behaviour to maximise their efficiency in different contexts."
The study used high-speed cameras to film flocks of wild jackdaws at sites in Cornwall, UK, to create 3D reconstructions of the movements of each bird in the flock.
The authors included Joe Westley, an Exeter undergraduate on the university's Access to Internships Scheme, and the project was funded by the Human Frontier Science Program.
The paper, published in Nature Communications, is entitled: "Behavioural plasticity and the transition to order in jackdaw flocks."