Ensuring the quality of milk is paramount for consumer health and industry standards. This study introduces a comparative analysis of two machine learning approaches, the k-Nearest Neighbors (KNN) algorithm and its variant, the Distance-Weighted KNN (DW-KNN), for the detection of milk quality. While the traditional KNN algorithm has been widely applied across various sectors for its simplicity and effectiveness, our research proposes an enhanced methodology through the implementation of the DW-KNN algorithm, which incorporates distance weighting to improve prediction accuracy. Through the analysis of a comprehensive dataset encompassing multiple milk quality indicators, we demonstrate that the DW-KNN algorithm significantly outperforms the standard KNN approach, achieving an exceptional accuracy of 99.53% compared to 98.58% by KNN. This substantial improvement highlights the potential of distance weighting in enhancing classification performance, particularly in applications requiring high precision in quality assessment. Our findings advocate for the adoption of the DW-KNN algorithm in the dairy industry and related fields, offering a robust tool for ensuring product quality and safety.