Problems/1. Bubble Sort
PrevNext

1. Bubble Sort

EasyArraySorting

Given an integer array nums, sort the array in ascending order using the bubble sort algorithm.

Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the entire list is sorted.

Example 1
Input: nums = [64, 34, 25, 12, 22, 11, 90]
Output: [11, 12, 22, 25, 34, 64, 90]
The elements are repeatedly compared and swapped until they are in ascending order.
Example 2
Input: nums = [5, 1, 4, 2, 8]
Output: [1, 2, 4, 5, 8]
Each pass bubbles the largest remaining unsorted element to its correct position at the end.
Visualizer

Visualizer will appear here