English 中文(简体)
根据逆时针滚动路径对平方矩阵数组的行进行排序
原标题:Sort rows of a square matrix array according to an anti-clockwise spiralling path
最佳回答
问题回答

这将有效。 您可以将任何数组传送到圆形Sort 函数, 它会返回您排序的数组 。

/*
Get the circular sorted array
*/
function circularSort($array)
{
    //Get the length of array
    $arrayLength = count($array);
    //Find the square root of length of array
    $arrayRows = sqrt($arrayLength);

    //Divide the arrays in $arrayRows
    $arrayChunks = array_chunk($array,$arrayRows);
    $circularArray = array();

    //Call Circular Array function .. Result will be stored in $circularArray
    circularArray($arrayChunks,$circularArray);
    return $circularArray;
}

/*
Loop arrayChunk in following order
1. Fetch first item from each chunks
2. Fetch all items from last chunk and remove that array from arrayChunk
3. Reverse elements in each remaining chunk
4. Reverse entire arrayChunk array
5. Repeat above 4 steps until $arrayChunks is empty
*/
function circularArray(&$arrayChunks, &$circularArray)
{
    if(empty($arrayChunks))
    {
        return true;
    }

    //1. Fetch first item from each chunks
    foreach($arrayChunks as &$arrayChunk)
    {
        $circularArray[] = array_shift($arrayChunk);
    }

    //Fetch Last Chunk from array
    $lastChunk = array_pop($arrayChunks);

    //2. Fetch all items from last chunk and remove that array from arrayChunk
    foreach($lastChunk as $chunkElement)
    {
        $circularArray[] = $chunkElement;
    }

    //3. Reverse elements in each remaining chunk
    foreach($arrayChunks as &$arrayChunk)
    {  
        if (is_array($arrayChunk))
        {    
            $arrayChunk = array_reverse($arrayChunk);
        }
    }

    $arrayChunks = array_reverse($arrayChunks);

    return circularArray(&$arrayChunks, &$circularArray);
}

例如:

$array = range(1, 25);
$circularArray = circularSort($array);

O(n) 中的另一种办法:

<?php
function circ_sort ($inArray) {

    $rowSize = pow(count($inArray), 0.5);
    if((int)$rowSize != $rowSize) {
        throw new InvalidArgumentException();
    }
    $rowSize = (int)$rowSize;

    $round =-1;
    for ($x =-1, $y=0, $count =0; $count < count($inArray);) {

        if ($y > $x) {
            if ($x +1 == $y) {
                $direction =  D ;   //Down
                $round ++;
                $max_iter = $rowSize - (2 * $round);
            } else {
                $direction =  L ; //Left
                $max_iter = $y - $x -1;
            }
        } else if ($x > $y) {
            $direction =  R ; //Right
            $max_iter = $rowSize - (2 * $round) -1;

        } else if ($x == $y) {
            $direction =  U ; //Up
            $max_iter = $rowSize - (2 * $round) -1;

        }

        switch ($direction) {
            case  D :   //Down
                for ($iter =0; $iter < $max_iter; $iter++) {
                    $x++;
                    $circArray[] = $inArray[$x*$rowSize + $y];
                    $count++;
                }
                break;
            case  R : //Right
                for ($iter =0; $iter < $max_iter; $iter++) {
                    $y++;
                    $circArray[] = $inArray[$x*$rowSize + $y];
                    $count++;
                }
                break;
            case  U :   //Up
                for ($iter =0; $iter < $max_iter; $iter++) {
                    $x--;
                    $circArray[] = $inArray[$x*$rowSize + $y];
                    $count++;
                }
                break;
            case  L :   //Left
                for ($iter =0; $iter < $max_iter; $iter++) {
                    $y--;
                    $circArray[] = $inArray[$x*$rowSize + $y];
                    $count++;
                }
                break;
        }
    }
    return ($circArray);
}

$array = range(1, 25);
$circ_array = circ_sort($array);

var_dump($circ_array);

?>

我的解决方案:

骗局是:第一个是5个,然后是2个,共4个元素,两个3个元素,2个元素,2个元素,2个元素,2个元素,2个元素。 (5,4,4,4,3,3,3,2,2,1,1,1) 在每个运行中,每个运行中增加一个状态模块4,视运行向一个方向或另一个方向运行的状态而定。

这是代码:

function circularSort(array $array) {
    $n2=count($array);
    $n=sqrt($n2);
    if((int)$n != $n) throw new InvalidArgumentException();

    $Result = Array();
    $run =$n; $dir=1;
    $x=0; $y=-1;
    $i=0;
    $st=0;
    while ($run) {
        while ($dir) {
            for ($j=0; $j<$run; $j++) {

                if ($st==0) $y++;
                if ($st==1) $x++;
                if ($st==2) $y--;
                if ($st==3) $x--;

                $p=$y * $n +$x;
                array_push($Result,$array[$p]);

            }
            $st = ($st +1) & 3;
            $dir--;
        }

        $dir=2;
        $run--;
    } 

    return $Result;
}

$a = range(1,25);
var_dump(circularSort($a));




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签