In wireless sensor networks (WSNs), conventional clustering routing algorithms often lead to uneven distribution of cluster heads (CHs), resulting in imbalanced and high-energy consumption. Addressing these challenges, this study introduces a novel algorithm, termed the energy distance function-based improved K-means algorithm (EDFIKM). This algorithm synthesizes the principles of the low-energy adaptive clustering hierarchy (LEACH) with the K-means clustering methodology. Key to this approach is the formulation of an adaptive weighted energy distance function that accounts for nodes' residual energy, their proximity to cluster centers, and dynamic weights for energy and distance factors. This function strategically mitigates the randomness in CH selection. Incorporating this function into the K-means clustering process achieves a more uniform distribution of CHs and an equitable allocation of nodes across clusters. Importantly, this contributes to a balanced energy load and a reduction in overall network energy consumption. Simulation studies validate that the EDFIKM algorithm efficiently lowers network energy demands and extends the network lifespan, without adding to the time complexity.