Implemented an optimized bubble sort