Peter gets a job in the supermarket. His job is to arrange the products according to the product numbers mention on each product. The products are in mixed order. He has to arrange them in ascending order. The products are given in the form of an array. The products at indices a and b (where a < b) form a transposition if arr[a] > arr[b]. To fix a transposition, he can interchange the adjacent products.
"Arr = [ 1, 2, 1, 4, 3]"
To sort the array, he swaps the products.
"Arr = [ 1, 1, 2, 3, 4]"
The sort has two transpositions (2,1) and (4,3).
Write a function
solve that should accept the following parameter:
1.) arr = an array of integers to sort
arr = [1,3,5,7]
This array is already sorted, no inversions needed.
arr = [4,2,8,6]
In this array,
"Swap 1 [2,4,8,6]"
"Swap 2 [2,4,6,8]"
Total "2" swaps to fix inversions.
• The number of elements in the array will be greater than 1 and less than 600.
• The size of each indices will be greater than 1 and less than 10000.