Посчитать количество обменов в Mergesort и Radixsort

Имеются вот такие реализации сортировки слиянием и поразрядной сортировки, возник большой вопрос, как можно посчитать в них количество свопов (обменов)? Если по большому счёту обменов в них нет.

public function mergesort($numlist)
    {
        if (count($numlist) == 1) return $numlist;

        $mid = count($numlist) / 2;
        $left = array_slice($numlist, 0, $mid);
        $right = array_slice($numlist, $mid);

        $left = $this->mergesort($left);
        $right = $this->mergesort($right);

        return $this->merge($left, $right);
    }

    private function merge($left, $right)
    {
        $result = array();
        $leftIndex = 0;
        $rightIndex = 0;

        while ($leftIndex < count($left) && $rightIndex < count($right)) {
            if ($left[$leftIndex] > $right[$rightIndex]) {

                $result[] = $right[$rightIndex];
                $rightIndex++;
            } else {
                $result[] = $left[$leftIndex];
                $leftIndex++;
            }
        }
        while ($leftIndex < count($left)) {
            $result[] = $left[$leftIndex];
            $leftIndex++;
        }
        while ($rightIndex < count($right)) {
            $result[] = $right[$rightIndex];
            $rightIndex++;
        }
        return $result;
    }
    public function radixSort()
    {
        //Create a bucket of arrays
        $bucket = array_fill(0, 9, array());
        $maxDigits = 0;
        //Determine the maximum number of digits in the given array.
        foreach ($this->array as $value) {
            $numDigits = strlen((string) $value);
            if ($numDigits > $maxDigits)
                $maxDigits = $numDigits;
        }
        $nextSigFig = false;
        for ($k = 0; $k < $maxDigits; $k++) {
            for ($i = 0; $i < count($this->array); $i++) {
                if (!$nextSigFig)
                    $bucket[$this->array[$i] % 10][] =  $this->array[$i];
                else
                    $bucket[floor(($this->array[$i] / pow(10, $k))) % 10][] =  $this->array[$i];
            }
            //Reset array and load back values from bucket.
            $this->array = array();
            for ($j = 0; $j < count($bucket); $j++) {
                foreach ($bucket[$j] as $value) {
                    $this->array[] = $value;
                }
            }
            //Reset bucket
            $bucket = array_fill(0, 9, array());
            $nextSigFig = true;
        }
    }

Ответы (0 шт):