#K38412. Bubble Sort Implementation

    ID: 26193 Type: Default 1000ms 256MiB

Bubble Sort Implementation

Bubble Sort Implementation

You are required to implement the bubble sort algorithm to sort a list of integers in non-decreasing order. Do not use any built-in sorting functions. The algorithm should repeatedly step through the list, compare adjacent pairs and swap them if they are in the wrong order. The process should be repeated until the list is sorted.

Note: Input is provided via standard input and output should be sent to standard output.

The bubble sort algorithm follows this mathematical formulation:

\(\text{for } i=0 \text{ to } n-1:\)

\(\quad \text{for } j=0 \text{ to } n-i-2:\)

\(\quad\quad \text{if } a_j > a_{j+1} \text{ then swap } a_j \text{ and } a_{j+1}\)

inputFormat

The input consists of two lines:

  • The first line contains an integer n, representing the number of elements in the list. (0 ≤ n ≤ 105)
  • The second line contains n space-separated integers.

If n is 0, the second line may be empty.

outputFormat

Output the sorted list of integers in non-decreasing order as a single line of space-separated integers. If the list is empty, output an empty line.

## sample
5
4 2 5 1 3
1 2 3 4 5

</p>