removing duplicate code and writing bubble sort method