In many medical researches, it is needed to determine the optimal sample size allocation in a heterogeneous population. This paper proposes the algorithm for optimal sample size allocation. We consider the optimal allocation problem as an optimization problem and the solution is obtained by using Bisection, Secant, Regula–Falsi and other numerical methods. The performance of the algorithm for different numerical methods are analyzed and evaluated in terms of computing time, number of iterations and gain in accuracy using stratification. The efficacy of algorithm is evaluated for the response in terms of body mass index (BMI) to the dietetic supplement with diabetes mellitus, HIV/AIDS and cancer post-operatory recovery patients.