//cluster heads selection
maxE=zeros(1,nc) ;
maxE is a row vector contains nc zeros
it_max: maximum number of iterations
ET: is the totalnetwork energy
while(it≤it_max || ET>0)
for i = 1 to nc do
if it==1
r=rand*length(c(i))
ch(i)=c(i).r ;
else
for j=1 to length(c(i)) do
if maxE(i)<c(i).E(j)
maxE(i)=c(i).E(j)
end if
end for
ch(i)=maxE(i)
end if
end for
Calculation of distances between CHs and distances b
tween CHs and BS
for i=1 to nc
for j=1 to nc do
end for
end for
for i=1 to nc do
if dBS(i)==mindBS
ch(i) sends directly to BS
else
for j=1 to nc do
Energyif
(i≠j && d(i,j)==min(i) && dBS(i)>dBS(j))
ch(i) sends data to ch(j)
end if
end for
end if
end for