Back
Close

Bubble Sort

Statement

 Goal

Bubble sort is a sorting algorithm that does multiple passes over an array. On each pass it compares adjacent elements starting from left and swaps them if they are in the wrong order. This lets smaller numbers "bubble" to the top, while larger numbers "sink" to the bottom. The algorithm completes when there is nothing left to swap on the last pass.

Given a list of numbers, return the resulting list after P passes of bubble sort (passing from left to right).

For example consider the following sequence after 2 passes of bubble sort algorithm.

5 4 3 2 1 --> 4 5 3 2 1 --> 4 3 5 2 1
Input
Line 1: N number of integers in the array
Line 2: A space-separated list of integers
Line 3: The number of passes P
Output
The (semi-) sorted list after P left-to-right passes of bubble sort (ascending), as a space-separated list on a single line.
Constraints
0 ≤ N ≤ 10
0 ≤ P ≤ 10
0 ≤ numbers ≤ 100
Example
Input
5
5 25 50 20 10
1
Output
5 25 20 10 50

Game modes
Fastest, Shortest

Test cases
One Pass Test
Input
5 5 25 50 20 10 1
Output
5 25 20 10 50

One Pass Validator
Input
5 4 35 61 22 15 1
Output
4 35 22 15 61

Handle Number 0 Correctly Test
Input
6 11 22 0 44 66 33 1
Output
11 0 22 44 33 66

Handle Number 0 Correctly Validator
Input
6 12 67 0 33 56 34 1
Output
12 0 33 56 34 67

Full Sort (Exact Number of Passes) Test
Input
10 14 24 87 52 1 34 5 2 61 33 6
Output
1 2 5 14 24 33 34 52 61 87

Full Sort (Exact Number of Passes) Validator
Input
10 17 28 81 54 4 38 9 1 67 39 6
Output
4 1 9 17 28 38 39 54 67 81

Zero Passes Test
Input
8 2 53 7 45 3 8 75 4 0
Output
2 53 7 45 3 8 75 4

Zero Passes Validator
Input
8 4 62 8 13 5 74 12 9 0
Output
4 62 8 13 5 74 12 9

Too Many Passes Test
Input
4 12 4 3 5 5
Output
3 4 5 12

Too Many Passes Validator
Input
4 6 32 9 5 10
Output
5 6 9 32

Handle Duplicate Numbers Test
Input
10 14 9 53 9 11 6 80 99 10 6 6
Output
6 9 9 6 10 11 14 53 80 99

Handle Duplicate Numbers Validator
Input
10 12 7 75 7 10 4 77 90 9 4 6
Output
4 7 7 4 9 10 12 75 77 90

Solution language

Solution

Stub generator input