Optimizing bubble sort