Use PHP to honor four common sorting algorithms and their implementation principles

Source: Internet
Author: User
Use PHP to implement four common sorting algorithms and their implementation principles & lt ;? PHP ***** insert sorting (one-dimensional array) 1. starting from the first element, this element can be considered to have been sorted 2, and the next element is taken out, in the sorted element sequence, scan 3 from the back to the front. if the element (sorted) is greater than the new element, move the element to the next position 4, reuse PHP to implement four common sorting algorithms and implementation principles


* ***** Insert sorting (one-dimensional array)

1. starting from the first element, this element can be considered to have been sorted

2. extract the next element and scan it forward from the sorted element sequence.

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

4. Repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements.

5. insert the new element to this 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 sorting (one-dimensional array)

1. compare adjacent elements. If the first is bigger than the second, exchange the two of them.

2. perform the same operation on each adjacent element, starting from the first pair to the last one. At this point, the final element should be the largest number.

3. repeat the preceding steps for all elements except the last one.

4. continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number.

*/

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 sorting (one-dimensional array)

1. First, find the minimum element in the unordered sequence and store it to the starting position of the sorting sequence,

2. Then, find the smallest element from the remaining unordered elements and put it at the end of the sorting sequence.

3. wait 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 sorting (one-dimensional array)

1. first, a random median value is obtained.

2. place a value smaller than the median value on the left and a value greater than the median value on the right,

3. then, recursively call step 1 and Step 2 for the left and right data respectively to merge the data on the left, center, and right.

*/

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,341 345, 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.