bubble sort optimized so that iteration won't be redundant