I have some code to sort an array into ascending order
int[] array = new int[] { 4, 3, 5, 1 };
var result = array.GroupBy(x => x)
.OrderBy(g => g.Count())
.ThenBy(g => g.Key)
.SelectMany(g => g);
I would like to be able to count the number of steps required to complete the sort. The specific question I am trying to solve is this: You are given an unordered array consisting of consecutive integers [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. You need to find the minimum number of swaps required to sort the array in ascending order.
Can I find the number of swaps used by LINQ?
Is it possible to get this?
So for example if the query had to swap 4 with 1 to get 1,3,5,4 then 5 with 4 to get 1,3,4,5 then this would be 2 steps.