Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Bubble Sort

Sortingeasy

Course · Section 11: Working With Arrays · Lecture 172: Sorting Arrays

Implement `bubbleSort(arr)` that returns a new-or-in-place array sorted in ascending order using bubble sort.

Sample tests

Input: bubbleSort([5,2,4,1])
Output: [1,2,4,5]
Input: bubbleSort([])
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Sorting in place mutates the caller's array; copy first if that matters.

Learning resources

  • MDN: Array.prototype.sort
Approach & explanation (try first)

Bubble sort makes repeated passes swapping neighbors. O(n^2) time, O(1) extra space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.