Using PHP to redeem common four kinds of sorting algorithms and implementation principle

Source: Internet
Author: User
Using PHP to implement four kinds of common sorting algorithms and its realization principle


Insert sort (one-dimensional array)

1, starting with the first element, the element can be thought to have been sorted

2, take the next element and scan it from the back forward in the sequence of elements already sorted

3, if the element (sorted) is greater than the new element, move the element to the next position

4, repeat step 3 until you find the sorted element is less than or equal to the position of the new element

5, insert a new element into the position

6, Repeat step 2

*/

function Insert_sort ($arr)

{

??? $len = count ($arr);

??? for ($i =1; $i < $len; $i + +)

??? {

??????? $tmp = $arr [$i];

??????? $j = $i-1;

??????? while ($arr [$j] > $tmp && $j >=0)

??????? {

??????????? $arr [$j +1] = $arr [$j];

??????????? $j--;

??????? }

??????? $arr [$j +1] = $tmp;

??? }

??? return $arr;

}

/*

Bubble sort (one-dimensional array)

1, compare adjacent elements. If the first one is bigger than the second one, swap them both.

2, for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number.

3, repeat the above steps for all elements except the last one.

4, repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.

*/

function Bubble_sort ($arr)

{

??? $len = count ($arr);

??? for ($i =0; $i < $len; $i + +)

??? {

??????? for ($j = $len-1; $j > $i; $j-)

??????? {

??????????? if ($arr [$j-1] > $arr [$j])

??????????? {

??????????????? $tmp = $arr [$j-1];

??????????????? $arr [$j-1] = $arr [$j];

??????????????? $arr [$j] = $tmp;

??????????? }

??????? }

??? }

??? return $arr;

}

/*

Select sort (one-dimensional array)

1, first find the smallest element in the unordered sequence, and place it in the starting position of the sort sequence.

2, then continue looking for the smallest element from the remaining unsorted elements, and place it at the end of the sort sequence.

3, and so on until all elements are sorted.

*/

function Select_sort ($arr) {

??? $count = count ($arr);

??? for ($i =0; $i < $count-1; $i + +)

??? {

??????? $k = $i;

??????? for ($j = $i +1; $j < $count; $j + +)

??????? {

??????????? if ($arr [$k] > $arr [$j])

??????????? {

??????????????? $k = $j;

??????????? }

??????? }

??????? if ($k! = $i)

??????? {

??????????? $tmp = $arr [$i];

??????????? $arr [$i] = $arr [$k];

??????????? $arr [$k] = $tmp;

??????? }

???? }

??? return $arr;

}

/*

Quick sort (one-dimensional array)

1, first randomly take an intermediate value

2, put the lower than the median value to the left, than the middle value of the large place on the right side,

3, and then the left and right data recursively call 1, 2 steps, merge left, middle value, right data.

*/

function Quick_sort ($arr)

{

??? if (count ($arr) <= 1)

??? {

??????? return $arr;

??? }

??? $key = $arr [0];

??? $left _arr = Array ();

??? $right _arr = Array ();

??? for ($i =1; $i

??? {

??????? if ($arr [$i] <= $key) $left _arr[] = $arr [$i];

??????? Else? $right _arr[] = $arr [$i];

??? }

??? $left _arr = Quick_sort ($left _arr);

??? $right _arr = Quick_sort ($right _arr);

??? Return Array_merge ($left _arr, $key, $right _arr);

}

$a = array (123,321,432,341345,45234,53,493);

echo "

";

Print_r (Select_sort ($a));

Print_r (Bubble_sort ($a));

Print_r (Insert_sort ($a));

Print_r (Quick_sort ($a));

echo "
";

  • 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.