PHP Data structure algorithm (PHP description) Simple Choice sort selection sort_php Tips

Source: Internet
Author: User
Copy code code as follows:

<?php
/**
* Simple-select sort Easy-selection sorts
*
* principle: once Each number in the selected array, make a note of the current position and assume it is the smallest number in the number of min=i from the current position, starting at the next number of this number until the last number, and recording the minimum position min, if min is not equal to I at the end of the scan, the assumption is wrong, The number of min and I positions is exchanged.
*/
Function sort_simple_selection ($list)
{
$len = count ($list);
if (empty ($len)) return $list;
for ($i = 0; $i < $len; $i + +)
{
$min = $i;
for ($j = $i + 1; $j < $len $j + +)
{
//if ($list [$j] > $list [$min])/from large to small
if ($list [$j] < $l ist[$min])//From small to large
{
$min = $j;
}
Echo implode (', ', $list). " #pos = ". ($min + 1). " Min= ". $list [$min]." <br/> ";
}
if ($min!= $i)
{
$temp = $list [$i];
$list [$i] = $list [$min];
$list [$min] = $temp;
}
Echo "-------------------------<br/>";
}
}
$list = Array (4,3,2,1,5,7,3,7);
$list = sort_simple_selection ($list);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.