Orig Description
Bubble Sort
Sorting algorithms are essential basic algorithms in computer science for arranging data. For example, sorting an array of integer values in ascending order is a sorting operation, as shown in the figure below.
Many sorting algorithms have been developed, but one of the basic algorithms is bubble sort. For example, let's try to arrange an given array of integers in ascending order using bubble sort.
In bubble sort, at each calculation step, the array is divided into a "sorted part" and an "unsorted part". At the beginning, the entire array becomes the unsorted part.
Starting from the beginning of the unsorted part, adjacent elements are compared (green elements in the figure), and they are swapped so that the larger value comes to the right. If the two values are equal, they are not swapped.
Repeat this process until the end of the unsorted part (white elements in the figure). Finally, add the end to the sorted part (blue elements), and one step is completed.
Repeat this step until the length of the unsorted part becomes 1.
When the length of the unsorted part becomes 1, the sorting process is finished.
Now, create a program that takes an array of n numbers as input and sorts them in ascending order from the beginning of the array using the above bubble sort procedure, and outputs the number of array element swaps required.
Input
The input consists of multiple datasets. The end of the input is indicated by a single zero.
Each dataset is given in the following format.
n
a1
a2
:
an
The first line contains the number of numbers n (1 ≤ n ≤ 100), and the following n lines contain the i-th number ai (1 ≤ ai ≤ 1000000).
The number of datasets does not exceed 20.
Output
For each dataset, output the number of swaps required to sort the data elements in one line.
Sample Input
5
5
3
2
1
4
6
1
2
3
4
5
6
3
3
2
1
0
Output for the Sample Input
7
0
3
Extracted Specification
An integer n (1 ≤ n ≤ 100), followed by n integers each on a new line (1 ≤ ai ≤ 1,000,000). The input consists of multiple such datasets. The end of the input is indicated by a single zero. The number of datasets does not exceed 20.
### Example Input:
```
5
5
3
2
1
4
6
1
2
3
4
5
6
3
3
2
1
0
```
### Function Signature:
Write a function f(inputs) that takes in the input.
```python
def f(inputs: List[Tuple[int, List[int]]]):
'''
inputs: a list of tuples where each tuple contains:
- an integer
- a list of integers
'''
```
Test Case Generator
Sure, I can help you create a test case generator for the described Bubble Sort problem. Here is a Python function that generates random test cases within the given constraints:
```python
import random
def generate_single_dataset():
# Generate a random integer n (1 ≤ n ≤ 100)
n = random.randint(1, 100)
# Generate n random integers (1 ≤ ai ≤ 1000000)
array = [random.randint(1, 1000000) for _ in range(n)]
return n, array
def tcgen():
datasets = []
# Number of datasets (up to 20)
num_datasets = random.randint(1, 20)
for _ in range(num_datasets):
n, array = generate_single_dataset()
datasets.append((n, array))
return datasets
def format_datasets(datasets):
result = []
for n, array in datasets:
result.append(f"{n}")
result.extend(map(str, array))
result.append("0") # End of input indicator
return "\n".join(result)
# Example usage
datasets = tcgen()
formatted_datasets = format_datasets(datasets)
print(formatted_datasets)
```
Here's a breakdown of the code:
1. **`generate_single_dataset` function**: This function generates a single dataset. It creates a random integer `n` (1 ≤ `n` ≤ 100) and then generates a list of `n` random integers (1 ≤ `ai` ≤ 1000000).
2. **`tcgen` function**: This function generates up to 20 datasets by calling `generate_single_dataset` repeatedly. Each dataset is appended to a list.
3. **`format_datasets` function**: This function formats the datasets into the required format for the Bubble Sort problem. It concatenates all datasets into a single string and adds the end-of-input indicator (`0`).
4. **Example usage**: The last part of the code demonstrates how to use these functions to generate and print the formatted datasets.
You can run the provided code to generate random test cases for the Bubble Sort problem.
Extract Arguments
def extract_arguments(fh):
datasets = []
while True:
n = int(fh.readline().strip())
if n == 0:
break
a = [int(fh.readline().strip()) for _ in range(n)]
datasets.append((n, a))
return datasets