#K38882. Bubble Sort Implementation
Bubble Sort Implementation
Bubble Sort Implementation
Implement the bubble sort algorithm to sort a list of integers in non-decreasing order. The bubble sort algorithm works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The process is repeated until the list is sorted.
Algorithm Overview:
Given an array \(A\) of \(n\) elements, the algorithm performs at most \(n-1\) passes. In each pass, adjacent elements are compared and swapped if necessary. The worst-case time complexity of bubble sort is \(O(n^2)\).
Note: You are not allowed to use any built-in sorting functions.
inputFormat
The input is read from stdin
and consists of two lines:
- The first line contains a single integer \(n\) representing the number of elements in the array.
- The second line contains \(n\) space-separated integers.
If \(n = 0\), the second line may be empty.
outputFormat
Output the sorted array in non-decreasing order on a single line to stdout
. The numbers should be separated by a single space. If the array is empty, output an empty line.
6
34 7 23 32 5 62
5 7 23 32 34 62
</p>