ExamAdda Logo

Bubble Sort

Easy

Given an integer array nums of size n, sort the array in ascending order using the Bubble Sort algorithm.

Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. After each pass, the largest unsorted element moves to its correct position.

Return the sorted array.

Example 1

Input

5
64 34 25 12 22

Output

12 22 25 34 64

Explanation

Bubble Sort repeatedly compares adjacent elements and swaps them whenever they are in the wrong order. After all passes, the array becomes sorted.

Constraints

1 ≤ n ≤ 10³
-10⁵ ≤ nums[i] ≤ 10⁵

Hints:

Hint 1

Compare every pair of adjacent elements. Swap them if they are in the wrong order.

Hint 2

After each pass, the largest unsorted element reaches its correct position. Continue until no more swaps are needed or all passes are completed.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Jul 21, 2026

Last updated

Aug 6, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output