Wireless sensor networks are battery-powered ad hoc networks in which sensor nodes that are scattered over a region connect to each other and form multi-hop networks. Since these networks consist of sensors that are battery operated, care has to be taken so that these sensors use energy efficiently. This paper proposes an optimized hierarchical routing technique which aims to reduce the energy consumption and prolong network lifetime. In this technique, the selection of optimal cluster head (CHs) locations is based on artificial fish swarm algorithm that applies various behaviors such as preying, swarming, and following to the formulated clusters and then uses a fitness function to compare the outputs of these behaviors to select the best CHs locations. To prove the efficiency of the proposed technique, its performance is analyzed and compared to two other well-known energy efficient routing techniques: low-energy adaptive clustering hierarchy (LEACH) technique and particle swarm optimized (PSO) routing technique. Simulation results show the stability and efficiency of the proposed technique. Simulation results show that the proposed method Communicated by V. Loia. Egypt outperforms both LEACH and PSO in terms of energy consumption, number of alive nodes, first node die, network lifetime, and total data packets received by the base station. This may be due to considering residual energies of nodes and their distance from base station , and alternating the CH role among cluster's members. Alternating the CH role balances energy consumption and saves more energy in nodes.