Back
Close

Bubble Sort

Statement

 Goal

Your task is to sort an array a
Note: Use the bubble sort algorithm

Bubble Sort is an algorithm for sorting a list of numbers, where pairs of adjacent elements are checked and swapped if they aren't in order.
For example, if the input is 4 3 2 1, then the first pass of Bubble Sort will be
4, 3, 2, 1 --> 3, 4, 2, 1
3, 4, 2, 1 --> 3, 2, 4, 1
3, 2, 4, 1 --> 3, 2, 1, 4
This pass made 3 swaps.
The next pass will be:
3, 2, 1, 4 --> 2, 3, 1, 4
2, 3, 1, 4 --> 2, 1, 3, 4
2, 1, 3, 4 (No swap)
This pass made 2 swaps.
The next pass is:
2, 1, 3, 4 --> 1, 2, 3, 4
1, 2, 3, 4 (No swap)
1, 2, 3, 4 (No swap)
This pass made 1 swap.
The next pass is:
1, 2, 3, 4 (No swap)
1, 2, 3, 4 (No swap)
1, 2, 3, 4 (No swap)
You can use built-in function also
Input
An array a
Output
Sort the array a using the bubble sort algorithm
Constraints
1 < a.length < 30
Example
Input
1 3 2 4 5
Output
1 2 3 4 5

Game modes
Fastest

Test cases
Test 1 Test
Input
1 3 2 4 5
Output
1 2 3 4 5

Validator 1 Validator
Input
5 3 2 4 1
Output
1 2 3 4 5

Test 2 Test
Input
100 1 2 3 54
Output
1 2 3 54 100

Validator 2 Validator
Input
100 1 2 3 51
Output
1 2 3 51 100

Test 3 Test
Input
100 1 2 3 510
Output
1 2 3 100 510

Validator 3 Validator
Input
100 1 2 3 45
Output
1 2 3 45 100

Test 4 Test
Input
2 3 2 3
Output
2 2 3 3

Validator 4 Validator
Input
2 3 3 4
Output
2 3 3 4

Solution language

Solution

Stub generator input