Cardiovascular disease stands as one of the primary contributors to global mortality, with the World Health Organization (WHO) reporting approximately 17.9 million deaths annually. Swift and accurate diagnosis of heart attacks is crucial to ensure timely and specialized intervention for patients afflicted by this ailment. A machine learning algorithm that can be employed for addressing such issues is the Random Forest algorithm. However, the efficacy of the model is significantly influenced by the features selected during the training phase. To mitigate this, the Binary Bat Algorithm (BBA) with greedy crossover has been utilized to enhance feature selection within the model. This approach is particularly adept at preventing convergence issues often associated with local minima. The optimal parameters for BBA with greedy crossover are determined to be , , , and . With these parameters, the proposed algorithm identifies the most relevant features, including age, gender, cp, chol, thalach, oldpeak, slope, and ca, achieving an accuracy of 94.19% on the training data and 91.8% on the test data. Furthermore, the precision and recall values for both classes range from 0.87 to 0.96, contributing to an approximate -score of 0.92. The proposed method has increased its -score by 0.05 if compared with the regular Random Forest model. These results underscore the effectiveness of the proposed algorithm in providing accurate and reliable predictions for heart disease diagnosis. As such, this model makes diagnosing heart attack more convenient and effective because it does not require too much medical features or patient data. Hopefully, the results of this research help medical practitioners make better and timely decisions in the diagnosis and treatment of heart attacks, as well as assist in planning more effective public health programs for heart attack prevention.