Skip to content
Onsite

Bubble Sort

EasyP2Coding

Sort an array of integers in ascending order using bubble sort and return the sorted array.

Bubble sort repeatedly compares adjacent elements and swaps them when they are out of order, so the largest remaining value "bubbles" to the end on each pass. The judge only checks the result, but write the algorithm yourself rather than calling a built-in sort.

Example 1

Input
nums = [5,2,9,1,5,6]
Output
[1,2,5,5,6,9]

Example 2

Input
nums = [3,2,1]
Output
[1,2,3]

Example 3

Input
nums = []
Output
[]

Constraints

  • 0 <= nums.length <= 1000
  • -10^4 <= nums[i] <= 10^4

Hints

Run checks the 3 visible tests, Submit all 9.