#C4488. Bubble Sort Swap Count
Bubble Sort Swap Count
Bubble Sort Swap Count
You are given an array of n integers. Your task is to simulate the bubble sort algorithm and count the total number of swap operations required to sort the array in ascending order.
Recall that bubble sort works by repeatedly stepping through the list, comparing adjacent pairs and swapping them if they are in the wrong order. In each pass, the algorithm makes fewer comparisons than the previous pass.
The number of swaps can be described by the formula:
$$\text{swaps} = \sum_{i=0}^{n-1}\sum_{j=0}^{n-i-2} \mathbf{1}_{a[j] > a[j+1]}$$
Implement a program that reads multiple test cases from stdin and for each test case, prints the number of swaps on a separate line to stdout.
inputFormat
The first line of input contains an integer \(t\) representing the number of test cases. Each test case is described in one line that starts with an integer \(n\) (the number of elements in the array) followed by \(n\) space-separated integers.
outputFormat
For each test case, output a single integer on a new line which is the total number of swaps performed by the bubble sort algorithm to sort the array in ascending order.
## sample3
5 1 3 5 2 4
5 5 4 3 2 1
3 2 3 1
3
10
2
</p>