fork download
  1. <?php
  2. //Дам массив чисел, отсортируйте по возрастанию без использования стандартной функции sort( ) и подобных. на JS/PHP.
  3. //Функция использует алгоритм сортировки выбором
  4. $numbers = array(1, 2, 3, 4, 5, 22, 12, 12, 123, 52, 24, 1302, -1);
  5.  
  6. function mySort(array $array, array $result = array())
  7. {
  8. if (empty($array)) {
  9. return $result;
  10. }
  11.  
  12. $keys = array_keys($array);
  13. $min = $array[$keys[0]];
  14. $minKey = $keys[0];
  15. foreach ($array as $key => $number) {
  16. if ($number < $min) {
  17. $min = $number;
  18. $minKey = $key;
  19. }
  20. }
  21. unset($array[$minKey]);
  22. $result[] = $min;
  23. return mySort($array, $result);
  24. }
  25.  
  26. print_r(mySort($numbers));
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Array
(
    [0] => -1
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 12
    [7] => 12
    [8] => 22
    [9] => 24
    [10] => 52
    [11] => 123
    [12] => 1302
)