The Bubble Up

EasyAcc. 98.1%
+15 XP 5

The Sinking Heaviness (Bubble)

In Bubble Sort, the heaviest values (large numbers) "sink" to the bottom of the array while the lighter values (small numbers) "bubble up" to the top. This is achieved by comparing every neighbors and swapping them if they are in the wrong order.

The Assignment

Your mission is to manually sort the array. Your function receives an array data.

  1. Use a nested loop structure.
  2. Compare adjacent elements: data[j] and data[j+1].
  3. If the left element is greater than the right, swap them.
  4. Print the final sorted array, where each number is separated by a single space.

01EXAMPLE 1

Input[5, 1, 4, 2, 8]
Output1 2 4 5 8

Explanation: Sorted in ascending order.

Constraints

  • O(n^2) average/worst time complexity.
AlgorithmsSorting
JavaScript
Loading...
1 Hidden

Input Arguments

data[5,1,4,2,8]

Expected Output

1 2 4 5 8

Click RUN to test your solution