Refactored bubblesort function.