#C5176. Bubble Sort Swap Count
Bubble Sort Swap Count
Bubble Sort Swap Count
Problem Statement:
Given an array of integers, your task is to determine the number of swaps performed by the Bubble Sort algorithm when sorting the array in increasing order. In Bubble Sort, adjacent elements are compared and swapped if they are in the wrong order, and this process is repeated until the array is sorted.
You can represent the number of swaps mathematically as:
$$\text{swaps} = \sum_{i=1}^{n-1} \sum_{j=0}^{n-i-1} [\text{distances}[j] > \text{distances}[j+1]]$$
Here, the Iverson bracket ([P]) equals 1 when the proposition (P) is true and 0 otherwise.
inputFormat
The input starts with an integer T, the number of test cases. For each test case:
1. The first line contains an integer N, the size of the array.
2. The second line contains N space-separated integers representing the array elements.
outputFormat
For each test case, output a single line with one integer that represents the number of swaps performed by Bubble Sort to sort the array.## sample
2
3
3 2 1
4
4 3 2 1
3
6
</p>