< Summary

Information
Class: LeetCode.Algorithms.SortAnArray.SortAnArrayMergeSort
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SortAnArray\SortAnArrayMergeSort.cs
Line coverage
89%
Covered lines: 42
Uncovered lines: 5
Coverable lines: 47
Total lines: 90
Line coverage: 89.3%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SortArray(...)100%11100%
SortArray(...)100%22100%
Merge(...)80%101085.29%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SortAnArray\SortAnArrayMergeSort.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.SortAnArray;
 13
 14/// <inheritdoc />
 15public class SortAnArrayMergeSort : ISortAnArray
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int[] SortArray(int[] nums)
 224    {
 225        SortArray(nums, 0, nums.Length - 1);
 26
 227        return nums;
 228    }
 29
 30    private static void SortArray(IList<int> array, int left, int right)
 1831    {
 1832        if (left >= right)
 1033        {
 1034            return;
 35        }
 36
 837        var middle = (left + right) / 2;
 38
 839        SortArray(array, left, middle);
 840        SortArray(array, middle + 1, right);
 841        Merge(array, left, middle, right);
 1842    }
 43
 44    private static void Merge(IList<int> array, int left, int middle, int right)
 845    {
 846        var leftSize = middle - left + 1;
 847        var rightSize = right - middle;
 48
 849        var leftArray = array.Skip(left).Take(leftSize).ToArray();
 850        var rightArray = array.Skip(middle + 1).Take(rightSize).ToArray();
 51
 852        var i = 0;
 853        var j = 0;
 854        var k = left;
 55
 2456        while (i < leftSize && j < rightSize)
 1657        {
 1658            if (leftArray[i] > rightArray[j])
 1159            {
 1160                array[k] = rightArray[j];
 61
 1162                j++;
 1163            }
 64            else
 565            {
 566                array[k] = leftArray[i];
 67
 568                i++;
 569            }
 70
 1671            k++;
 1672        }
 73
 1674        while (i < leftSize)
 875        {
 876            array[k] = leftArray[i];
 77
 878            i++;
 879            k++;
 880        }
 81
 882        while (j < rightSize)
 083        {
 084            array[k] = rightArray[j];
 85
 086            j++;
 087            k++;
 088        }
 889    }
 90}