44.8k views
3 votes
4. What is an inversion in a permutation? What is the relationship between the running time of bubble sort and number of inversions in the input array? Find the number of

inversions in

1 2 3 4 5

5 3 4 1 2

User Stockfisch
by
8.3k points

1 Answer

3 votes

Final answer:

An inversion in a permutation is a pair of elements that are in reverse order. The number of inversions in a permutation can indicate how out-of-order the permutation is. The running time of bubble sort is affected by the number of inversions in the input array.

Step-by-step explanation:

What is an inversion in a permutation?

In combinatorial mathematics, an inversion in a permutation is a pair of elements that are in reverse order with respect to their positions in the original permutation. For example, in the permutation (3, 1, 4, 2), the pairs (3, 1) and (4, 2) are inversions. The number of inversions in a permutation can be used as a measure of how out-of-order the permutation is.

Relationship between bubble sort's running time and number of inversions

In bubble sort, the number of inversions in the input array affects the running time of the algorithm. Bubble sort compares adjacent elements and swaps them if they are in the wrong order. Each swap reduces the number of inversions in the array. The worst-case running time of bubble sort is O(n^2), where n is the number of elements in the array. The worst case occurs when the array is sorted in reverse order, resulting in the maximum number of inversions.

Finding the number of inversions in the input array

To find the number of inversions in the array (1, 2, 3, 4, 5, 3, 4, 1, 2), we can iterate through each element and count the number of elements that come after it and are smaller than it. In this case, we have the following inversions: (5, 3), (5, 4), (5, 3), (5, 4), (5, 1), (5, 2), (3, 1), (4, 1), (4, 2), (3, 1), (3, 2), (4, 2). So the total number of inversions is 12.

User Bilal Awan
by
8.7k points