Sort with bubble sort
Sort N integers in ascending order using bubble sort (compare neighbours and swap them when they are out of order), and print the sorted list. Also print how many swaps were made.
Reported in: TCS NQT (3 May 2024) (memory-based)
Input
First line: N. Second line: N integers.
Output
First line: the sorted numbers. Second line: the number of swaps.
Constraints
1 ≤ N ≤ 1000.
Example 1
Input:
5
64 25 12 22 11
Output:
11 12 22 25 64
9Example 2
Input:
4
1 2 3 4
Output:
1 2 3 4
0Hint: Repeat passes over the list, swapping neighbours that are out of order and counting the swaps; stop when a pass makes no swap.