0

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.

Gribbler
  • 414
  • 1
  • 6
  • 21
  • 3
    "count the number of steps required to complete the sort" - could you give an example output based on your array input – Anu Viswan Dec 05 '18 at 14:32
  • What do you define as "step"? – Yeldar Kurmangaliyev Dec 05 '18 at 14:33
  • It's still unclear. You want to know the complexity of this query? https://stackoverflow.com/questions/11032015/how-to-find-time-complexity-of-an-algorithm – Tim Schmelter Dec 05 '18 at 14:37
  • The 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. – Gribbler Dec 05 '18 at 14:38
  • Ahhh, assuming this is homework the assignment is to implement a sorting algorithm and see how fast(minimum steps) you can make it. If that is the case, the teacher/professor doesn't want you to use LINQ. – Sam W Dec 05 '18 at 14:40
  • That was what I suspected... – Gribbler Dec 05 '18 at 14:41
  • 1
    But if the array consist of consecutive integers than it is already in ascending order. – Magnus Dec 05 '18 at 14:46
  • 1
    Well you can just implement Quicksort and then count when swapping is done. https://www.w3resource.com/csharp-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-9.php – Magnus Dec 05 '18 at 14:54
  • What do you mean by 'count when swapping is done' ? – Gribbler Dec 05 '18 at 15:31
  • 1
    You need to implement a sorting algorithm and count how many operations(or "swaps") it takes for your algorithm to sort the array. – Sam W Dec 05 '18 at 15:36
  • 1
    @Gribbler I deleted my previous test code. Checkout the new answer; I could not do it with linq but with our own sorting algorithm. – N Subedi Dec 05 '18 at 15:46
  • @Gribbler I mean you count when an item is assigned a new position in the array. This is done in one place in the algorithm linked, Im sure you can find it. – Magnus Dec 06 '18 at 12:00

1 Answers1

1

This is how I came up. I couldn't find the way using it with LINQ however with our own Sorting Algorithm. I did something like this:

static void Main(string[] args)
    {
        ObservableCollection<int> array = new ObservableCollection<int>() {4,3,1,5 };
        int steps = 0;
        array.CollectionChanged+= (sender, e) => 
        {
            Console.WriteLine($"{e.Action} : {string.Join(",", array) }" );
            steps++;
        };

        bool didSwap;
        do
        {
            didSwap = false;
            for (int i = 0; i < array.Count - 1; i++)
            {
                if (array[i] > array[i + 1])
                {
                    int temp = array[i + 1];
                    array[i + 1] = array[i];
                    array[i] = temp;
                    didSwap = true;
                }
            }
        } while (didSwap);


        Console.WriteLine("Sorted Result :");
        foreach(var item in array)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine($"Total Swapps {steps / 2}");

        Console.ReadLine();
    }

This is the output:

enter image description here

N Subedi
  • 2,858
  • 2
  • 22
  • 36