예제 #1
0
        public List <int> countSmaller(int[] nums)
        {
            NumberIndex[] cnums = new NumberIndex[nums.Length];
            for (int i = 0; i < nums.Length; i++)
            {
                cnums[i] = new NumberIndex(nums[i], i);
            }
            int[] smaller = new int[nums.Length];
            cnums = sort(cnums, smaller);
            List <int> res = new List <int>();

            foreach (int i in smaller)
            {
                res.Add(i);
            }
            return(res);
        }
예제 #2
0
        private NumberIndex[] sort(NumberIndex[] nums, int[] smaller)
        {
            int half = nums.Length / 2;

            if (half > 0)
            {
                int           i         = 0;
                NumberIndex[] rightPart = new NumberIndex[nums.Length - half];
                NumberIndex[] leftPart  = new NumberIndex[half];
                for (i = 0; i < leftPart.Length; i++)
                {
                    leftPart[i] = new NumberIndex(nums[i]);
                }

                for (i = 0; i < rightPart.Length; i++)
                {
                    rightPart[i] = new NumberIndex(nums[half + i]);
                }

                NumberIndex[] left = sort(leftPart, smaller), right = sort(
                    rightPart, smaller);
                int m = left.Length, n = right.Length;
                i = 0;
                int j = 0;
                while (i < m || j < n)
                {
                    if (j == n || i < m && left[i].number <= right[j].number)
                    {
                        nums[i + j]             = left[i];
                        smaller[left[i].index] += j;
                        i++;
                    }
                    else
                    {
                        nums[i + j] = right[j];
                        j++;
                    }
                }
            }
            return(nums);
        }
예제 #3
0
 public NumberIndex(NumberIndex another)
 {
     this.number = another.number;
     this.index  = another.index;
 }