$min = $i;
        for ($j = $i + 1; $j < $size; $j++) {
            if ($array[$j] < $array[$min]) {
                $min = $j;
            }
        }
        $temp = $array[$min];
        $array[$min] = $array[$i];
        $array[$i] = $temp;
    }
    return $array;
}
//Microtime function to set start time
$time_start = microtime(true);
//Printing the Array with commas
echo implode(", ", selection_sort($sortingArray));
//Stopping Microtime clock
$time_end = microtime(true);
$time = $time_end - $time_start;
//Printing result of Microtime function
echo "<br><br>Did sort in " . $time . " seconds\n";
?>
		</div>
		<div id="both_max_min">
			<h2>Selection Sort: Switching BOTH Max and Min:</h2>
			<?php 
//Setting a Random Array Set of 100 Values
$sortingArray2 = [];
for ($num = 1; $num <= 100; $num++) {
    array_push($sortingArray2, rand(1, 10000));
}
Exemple #2
0
$sample = array();
for ($i = 0; $i < 100; $i++) {
    $sample[] = rand(0, 10000);
}
// var_dump($sample);
function selection_sort($array)
{
    for ($i = 0; $i < count($array); $i++) {
        $min_key = null;
        $min_value = null;
        $max_key = null;
        $max_value = null;
        for ($j = $i; $j < count($array); $j++) {
            if ($min_value == null || $array[$j] < $min_value) {
                $min_key = $j;
                $min_value = $array[$j];
            }
            if ($max_value == null || $array[$j] > $max_value) {
                $max_key = $j;
                $max_value = $array[$j];
            }
        }
        $array[$min_key] = $array[$i];
        $array[$i] = $min_value;
        $array[$max_key] = $array[$i];
        $array[$i] = $max_value;
    }
    return $array;
}
$sorted_array = selection_sort($sample);
var_dump($sorted_array);
    posição e assim sucessivamente, até que a sequência esteja ordenada.
    É uma excelente escolha quando há necessidade quando o custo de 
    escrita é alto, pois ele realiza em torno de 2n operações de escrita. [2]
Complexidade de tempo: 
    O(n²)
Dificuldade: 
    facil
Referências:
    [1] http://en.wikipedia.org/wiki/Selection_sort
    [2] http://en.wikipedia.org/wiki/Selection_sort#Comparison_to_other_sorting_algorithms
*/
function selection_sort($a)
{
    for ($i = 0; $i < count($a) - 1; $i++) {
        $min = $i;
        for ($j = $i + 1; $j < count($a); $j++) {
            if ($a[$j] < $a[$min]) {
                $min = $j;
            }
        }
        $temp = $a[$i];
        $a[$i] = $a[$min];
        $a[$min] = $temp;
    }
    return $a;
}
$a = array(6, -2, 3, 5, 7, 4, 3);
$a = selection_sort($a);
for ($i = 0; $i < 7; $i++) {
    echo $a[$i] . "<br>";
}
 public function testOrderCharacterArray()
 {
     $a = ['a', 'b', 'c', 'd', 'e', 'f'];
     $b = ['a', 'b', 'c', 'd', 'e', 'f'];
     $this->assertEquals(selection_sort($a), $b);
 }
    $min = $arr[0];
    foreach ($arr as $key => $values) {
        $min = $arr[$key];
        for ($i = $key; $i < count($arr); $i++) {
            if ($arr[$i] < $min) {
                $min = $arr[$i];
                $min_index = $i;
            }
        }
        //Swap red with current list
        $temp = $arr[$key];
        $arr[$min_index] = $temp;
        $arr[$key] = $min;
        //		$min = $arr[$key+1];
    }
    var_dump($arr);
}
$test_array = array();
for ($i < 0; $i <= 100; $i++) {
    array_push($test_array, rand(1, 10000));
    //	microtime(selection_sort())
}
selection_sort($test_array);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Time selection sort algorithm took was {$time} seconds";
?>



{
    $len = count($array);
    $i = 0;
    while ($i < $len) {
        $min = $i;
        $max = $i;
        for ($j = $i; $j < $len; $j++) {
            if ($array[$j] < $array[$min]) {
                $min = $j;
            }
        }
        //swap array[j] and array[min]
        $tmp = $array[$min];
        $array[$min] = $array[$i];
        $array[$i] = $tmp;
        $i++;
    }
    return $array;
}
//demo
$x = array();
for ($i = 0; $i < 100; $i++) {
    array_push($x, rand(0, 10000));
}
$start = microtime(true);
$y = selection_sort($x);
$end = microtime(true);
$time_used = $end - $start;
echo "time used to sort: {$time_used} seconds<br>";
var_dump($x);
var_dump($y);
    return $arr;
}
function selection_sort($arr)
{
    $time_start = microtime(true);
    for ($x = 0; $x < count($arr); $x++) {
        $min = $arr[$x];
        $max = $arr[$x];
        for ($i = $x; $i < count($arr); $i++) {
            if ($arr[$i] < $min) {
                $min = $arr[$i];
                $arr = array_swap($arr, $i, $x);
            } else {
                if ($arr[$i] > $max) {
                    $max = $arr[$i];
                    $arr = array_swap($arr, count($arr) - 1, $i);
                }
            }
        }
    }
    var_dump($arr);
    $time_stop = microtime(true);
    echo $time_stop - $time_start;
}
$arr = array();
for ($o = 1; $o <= 10000; $o++) {
    $i = rand(1, 10000);
    $arr[] = $i;
}
selection_sort($arr);
    }
    return $arr;
}
function arr_with_10000_rand()
{
    $arr = array();
    for ($i = 0; $i < 10001; $i++) {
        array_push($arr, rand(0, 10000));
    }
    return $arr;
}
$start_time = microtime(true);
// $my_arr = array(8, 5, 2, 6, 9, 3, 1, 4, 0, 7);
// $my_arr2 = selection_sort($my_arr);
// $my_arr3 = modified_selection_sort($my_arr);
// $my_arr4 = arr_with_100_rand();
// $my_arr5 = selection_sort($my_arr4);
// $my_arr6 = modified_selection_sort($my_arr4);
$my_arr7 = arr_with_10000_rand();
$my_arr8 = selection_sort($my_arr7);
// took 72 seconds
// $my_arr9 = modified_selection_sort($my_arr7); //took 43 seconds
$end_time = microtime(true);
$total_time = $end_time - $start_time;
echo "It took " . round($total_time) . " seconds to execute";
// var_dump($my_arr);
// var_dump($my_arr2);
// var_dump($my_arr3);
// var_dump($my_arr4);
// var_dump($my_arr5);
// var_dump($my_arr6);
function selection_sort($arr)
{
    $last = count($arr) - 1;
    $time_start = microtime(true);
    for ($i = 0; $i < count($arr) - $i; $i++) {
        $min_idx = $i;
        $max_idx = $last - $i;
        for ($j = $i; $j < count($arr) - $i; $j++) {
            if ($arr[$min_idx] > $arr[$j]) {
                $min_idx = $j;
            }
            if ($arr[$max_idx] < $arr[$j]) {
                $max_idx = $j;
            }
        }
        swap($i, $min_idx, $arr);
        // swap the minimun to the head of the array;
        if ($i == $max_idx) {
            $max_idx = $min_idx;
        }
        swap($last - $i, $max_idx, $arr);
        // swap the maximum to the end of the array;
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "<font color='red'>The sort took: " . $time . " second(s).</font><br>";
    echo "<b>Sorted Array: <br></b>";
    var_dump($arr);
}
selection_sort($numbers);
                        $smallest_value = $arr[$j];
                    }
                    // end if beta
                }
                // third for end
            }
            // end minmax false
        }
        //end second for
        $arr[$smallest_index] = $arr[$i];
        $arr[$i] = $smallest_value;
    }
    //top for end
    return $arr;
}
//function end
$build_array = array();
for ($x = 1; $x <= 10000; $x++) {
    $build_array[] = rand(0, 10000);
}
var_dump($build_array);
$time_start = microtime();
$build_array = selection_sort($build_array, "min");
$time_end = microtime();
$time = $time_end - $time_start;
echo $time;
var_dump($build_array);
?>

</body>
</html>
<?php

// selection sort
// lets go
function selection_sort($list = array())
{
    // some validation
    if (!is_array($list)) {
        return false;
    }
    for ($i = 0; $i < count($list); $i++) {
        $min = $i;
        // assume 'i' is the min for now
        for ($j = $i + 1; $j < count($list); $j++) {
            // now check if there is any other element lesser than i
            $min = $list[$j] < $list[$min] ? $j : $min;
        }
        $temp = $list[$min];
        $list[$min] = $list[$i];
        $list[$i] = $temp;
    }
    // return the sorted array
    return $list;
}
$list = [7, 2, 5, 3, 8, 1];
$sorted_list = selection_sort($list);
echo implode(",", $sorted_list);