예제 #1
0
 private void Sort(List <int> list)
 {
     for (int i = 1; i < list.Count; i++)
     {
         int value = list[i];
         int j     = i - 1;
         while (j >= 0 && list[j] > value)
         {
             list[j + 1] = list[j];
             SetCallback?.Invoke(j + 1, list[j]);
             j--;
         }
         list[j + 1] = value;
         SetCallback?.Invoke(j + 1, value);
     }
 }
예제 #2
0
        private void Merge(List <int> list, int left, int middle, int right)
        {
            List <int> leftList  = list.GetRange(left, middle - left + 1);
            List <int> rightList = list.GetRange(middle + 1, right - middle);

            int listIndex  = left;
            int leftIndex  = 0;
            int rightIndex = 0;

            while (leftIndex < leftList.Count && rightIndex < rightList.Count)
            {
                if (leftList[leftIndex] <= rightList[rightIndex])
                {
                    list[listIndex] = leftList[leftIndex];
                    SetCallback?.Invoke(listIndex, leftList[leftIndex]);
                    leftIndex++;
                }
                else
                {
                    list[listIndex] = rightList[rightIndex];
                    SetCallback?.Invoke(listIndex, rightList[rightIndex]);
                    rightIndex++;
                }
                listIndex++;
            }

            while (leftIndex < leftList.Count)
            {
                list[listIndex] = leftList[leftIndex];
                SetCallback?.Invoke(listIndex, leftList[leftIndex]);
                leftIndex++;
                listIndex++;
            }

            while (rightIndex < rightList.Count)
            {
                list[listIndex] = rightList[rightIndex];
                SetCallback?.Invoke(listIndex, rightList[rightIndex]);
                rightIndex++;
                listIndex++;
            }
        }