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.
- Use a nested loop structure.
- Compare adjacent elements:
data[j]anddata[j+1]. - If the left element is greater than the right, swap them.
- Print the final sorted array, where each number is separated by a single space.
01EXAMPLE 1
Input
[5, 1, 4, 2, 8]Output
1 2 4 5 8Explanation: Sorted in ascending order.
Constraints
- O(n^2) average/worst time complexity.
AlgorithmsSorting
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
data[5,1,4,2,8]
Expected Output
1 2 4 5 8
Click RUN to test your solution