Skip to main content

Warm-up sorting · arrays

Bubble up

The slowest sort there is, and the best one to watch.

Write bubbleSort(nums): return a new array with the numbers in ascending order, using bubble sort. Walk the array, swap any neighbours that are out of order, and repeat until a full pass makes no swaps.

Call show(arr) after each swap and the stage will play your sort back as moving bars.

Example: bubbleSort([3, 1, 2]) returns [1, 2, 3].

Tests you can see

  • bubbleSort([3,1,2])[1,2,3]
  • bubbleSort([1,2,3,4])[1,2,3,4]

More tests are hidden. The server runs them all before a time counts.

Call show() while your code works and the run plays back here.

javascriptbubbleSort()

Tab indents · Cmd/Ctrl+Enter runs