[ Insider_m @ 22.03.2012. 23:27 ] @
| Kako sortirati prethodnije definiran array, tako da na kraju bi dobili ovo:

najmanji elemenat u nizu da bude u sredinu, i sve ostale da se rede u formu na zmiju(kao na sliku), tako da poslednji i najveci elemenat bude na pocetak od kocku. |
[ pajaja @ 23.03.2012. 03:37 ] @
Ovako nesto:
Code (php):
<?php
class SnakeOrder
{
private $size;
private $array;
public $matrix;
private $position;
private $iteration;
public function __construct
($array) {
$this->size = (int
)sqrt(sizeof($array));
$this->array = $array;
$this->matrix = array (array ());
$this->position = sizeof($this->array);
$this->iteration = 0;
}
public function makeMatrix
() {
$this->fill(-1, 0, $this->size);
}
private function fill
($x, $y, $length) {
$i = $dx = $dy = 0;
$axis = ($this->size+1)/2-1;
if ($x<=$axis && $y <= $axis) {
$dx = 1;
} else if ($x>=$axis && $y<=$axis) {
$dy = 1;
} else if ($x>=$axis && $y>=$axis) {
$dx = -1;
} else if ($x<=$axis && $y>=$axis) {
$dy = -1;
}
for ($i = 1; $i <= $length; $i++) {
$this->matrix[$y+$i*$dy][$x+$i*$dx] = $this->array[--$this->position];
}
if ($this->position == 0) {
return;
}
if (($this->iteration++) % 2 == 0) {
$length--;
}
$this->fill((int
)($x+($i-1)*$dx), (int
)($y+($i-1)*$dy), $length);
}
}
?>
Konstruktoru klase prosledjujes sortirtiran niz u rastucem poretku, i sa
makeMatrix() napravis matricu.
Copyright (C) 2001-2025 by www.elitesecurity.org. All rights reserved.