How can I sort arrays and data in PHP? -


due enormous , ever repeating amount of "how sort unique snowflake of array?" questions, reference collection of basic sorting methods in php. please close question not markedly differ duplicate of one.

how sort array in php?
how sort complex array in php?
how sort array of objects in php?


  1. basic 1 dimensional arrays; incl. multi dimensional arrays, incl. arrays of objects; incl. sorting 1 array based on another

  2. sorting spl

  3. stable sort

for practical answer using php's existing functions see 1., academic in-detail answer on sorting algorithms (which php's functions implement , may need really, complex cases), see 2.

well basic methods covered deceze try @ other types of sort

sorting spl

splheap

class simpleheapsort extends splheap {     public function compare($a, $b) {         return strcmp($a, $b);     } }  // let's populate our heap here (data of 2009) $heap = new simpleheapsort(); $heap->insert("a"); $heap->insert("b"); $heap->insert("c");  echo implode(php_eol, iterator_to_array($heap)); 

output

c b 

splmaxheap

the splmaxheap class provides main functionalities of heap, keeping maximum on top.

$heap = new splmaxheap(); $heap->insert(1); $heap->insert(2); $heap->insert(3); 

splminheap

the splminheap class provides main functionalities of heap, keeping minimum on top.

$heap = new splminheap (); $heap->insert(3); $heap->insert(1); $heap->insert(2); 

other types of sort

bubble sort

from wikipedia article on bubble sort:

bubble sort, incorrectly referred sinking sort, simple sorting algorithm works repeatedly stepping through list sorted, comparing each pair of adjacent items , swapping them if in wrong order. pass through list repeated until no swaps needed, indicates list sorted. algorithm gets name way smaller elements "bubble" top of list. because uses comparisons operate on elements, comparison sort. although algorithm simple, of other sorting algorithms more efficient large lists.

function bubblesort(array $array) {     $array_size = count($array);     for($i = 0; $i < $array_size; $i ++) {         for($j = 0; $j < $array_size; $j ++) {             if ($array[$i] < $array[$j]) {                 $tem = $array[$i];                 $array[$i] = $array[$j];                 $array[$j] = $tem;             }         }     }     return $array; } 

selection sort

from the wikipedia article on selection sort:

in computer science, selection sort sorting algorithm, in-place comparison sort. has o(n2) time complexity, making inefficient on large lists, , performs worse similar insertion sort. selection sort noted simplicity, , has performance advantages on more complicated algorithms in situations, particularly auxiliary memory limited.

function selectionsort(array $array) {     $length = count($array);     for($i = 0; $i < $length; $i ++) {         $min = $i;         for($j = $i + 1; $j < $length; $j ++) {             if ($array[$j] < $array[$min]) {                 $min = $j;             }         }         $tmp = $array[$min];         $array[$min] = $array[$i];         $array[$i] = $tmp;     }     return $array; } 

insertion sort

from wikipedia article on insertion sort:

insertion sort simple sorting algorithm builds final sorted array (or list) 1 item @ time. less efficient on large lists more advanced algorithms such quicksort, heapsort, or merge sort. however, insertion sort provides several advantages:

function insertionsort(array $array) {     $count = count($array);     for($i = 1; $i < $count; $i ++) {          $j = $i - 1;         // second element of array         $element = $array[$i];         while ( $j >= 0 && $array[$j] > $element ) {             $array[$j + 1] = $array[$j];             $array[$j] = $element;             $j = $j - 1;         }     }     return $array; } 

shellsort

from wikipedia article on shellsort:

shellsort, known shell sort or shell's method, in-place comparison sort. generalizes exchanging sort, such insertion or bubble sort, starting comparison , exchange of elements elements far apart before finishing neighboring elements.

function shellsort(array $array) {     $gaps = array(             1,             2,             3,             4,             6     );     $gap = array_pop($gaps);     $length = count($array);     while ( $gap > 0 ) {         for($i = $gap; $i < $length; $i ++) {             $tmp = $array[$i];             $j = $i;             while ( $j >= $gap && $array[$j - $gap] > $tmp ) {                 $array[$j] = $array[$j - $gap];                 $j -= $gap;             }             $array[$j] = $tmp;         }         $gap = array_pop($gaps);     }     return $array; } 

comb sort

from the wikipedia article on comb sort:

comb sort relatively simple sorting algorithm designed wlodzimierz dobosiewicz in 1980. later rediscovered stephen lacey , richard box in 1991. comb sort improves on bubble sort.

function combsort(array $array) {     $gap = count($array);     $swap = true;     while ( $gap > 1 || $swap ) {         if ($gap > 1)             $gap /= 1.25;         $swap = false;         $i = 0;         while ( $i + $gap < count($array) ) {             if ($array[$i] > $array[$i + $gap]) {                 // swapping elements.                 list($array[$i], $array[$i + $gap]) = array(                         $array[$i + $gap],                         $array[$i]                 );                 $swap = true;             }             $i ++;         }     }     return $array; } 

merge sort

from the wikipedia article on merge sort:

in computer science, merge sort (also commonly spelled mergesort) o(n log n) comparison-based sorting algorithm. implementations produce stable sort, means implementation preserves input order of equal elements in sorted output

function mergesort(array $array) {     if (count($array) <= 1)         return $array;      $left = mergesort(array_splice($array, floor(count($array) / 2)));     $right = mergesort($array);      $result = array();      while ( count($left) > 0 && count($right) > 0 ) {         if ($left[0] <= $right[0]) {             array_push($result, array_shift($left));         } else {             array_push($result, array_shift($right));         }     }     while ( count($left) > 0 )         array_push($result, array_shift($left));      while ( count($right) > 0 )         array_push($result, array_shift($right));      return $result; } 

quicksort

from the wikipedia article on quicksort:

quicksort, or partition-exchange sort, sorting algorithm developed tony hoare that, on average, makes o(n log n) comparisons sort n items. in worst case, makes o(n2) comparisons, though behavior rare.

function quicksort(array $array) {     if (count($array) == 0) {         return $array;     }     $pivot = $array[0];     $left = $right = array();     for($i = 1; $i < count($array); $i ++) {         if ($array[$i] < $pivot) {             $left[] = $array[$i];         } else {             $right[] = $array[$i];         }     }     return array_merge(quicksort($left), array(             $pivot     ), quicksort($right)); } 

permutation sort

from the wikipedia article on permutation sort:

permutation sort, proceeds generating possible permutations of input array/list until discovering sorted one.

function permutationsort($items, $perms = array()) {     if (empty($items)) {         if (inorder($perms)) {             return $perms;         }     } else {         for($i = count($items) - 1; $i >= 0; -- $i) {             $newitems = $items;             $newperms = $perms;             list($foo) = array_splice($newitems, $i, 1);             array_unshift($newperms, $foo);             $res = permutationsort($newitems, $newperms);             if ($res) {                 return $res;             }         }     } }  function inorder($array) {     for($i = 0; $i < count($array); $i ++) {         if (isset($array[$i + 1])) {             if ($array[$i] > $array[$i + 1]) {                 return false;             }         }     }     return true; } 

radix sort

from the wikipedia article on radix sort:

in computer science, radix sort non-comparative integer sorting algorithm sorts data integer keys grouping keys individual digits share same significant position , value.

// radix sort 0 256 function radixsort($array) {     $n = count($array);     $partition = array();      for($slot = 0; $slot < 256; ++ $slot) {         $partition[] = array();     }      for($i = 0; $i < $n; ++ $i) {         $partition[$array[$i]->age & 0xff][] = &$array[$i];     }      $i = 0;      for($slot = 0; $slot < 256; ++ $slot) {         for($j = 0, $n = count($partition[$slot]); $j < $n; ++ $j) {             $array[$i ++] = &$partition[$slot][$j];         }     }     return $array; } 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -