Improved bubble sort implementation