• + 0 comments

    I've implemented BubbleSort algorithm in C#, to count the swaps.

    OBS; this is not the best solution. O(n^2) time complexity.

    public static void minimumBribes(List<int> q)
        {
            if (q is null || q.Count() < 1)
            {
                Console.WriteLine(0);
                return;
            }
            
            int count = 0;
            int limit = 0;
            long currentValue = 0;
            
            for (int i = 0; i < q.Count(); i++)
            {
                for (int j = 1; j < q.Count() - i; j++)
                {
                    if (q[j-1] > q[j])
                    {
                        if (currentValue != q[j-1])
                        {
                            limit = 1;
                            currentValue = q[j-1];
                        }
                        else
                            limit++;
                        
                        if (limit > 2)
                        {
                            Console.WriteLine("Too chaotic");
                            return;
                        }
                        
                        (q[j], q[j-1]) = (q[j-1], q[j]);
                        count++;
                    }
                }
            }
            
            Console.WriteLine(count);
        }