Implement a basic sorting algorithm from scratch and discuss how you would optimize it for massive datasets.