PHP Sort: Algorithm idea and algorithm implementation of PHP insertion sorting

Source: Internet
Author: User
This article brings you the content is about PHP sort: php insertion sorting algorithm idea and algorithm implementation, there is a certain reference value, the need for friends can refer to, I hope to help you.

Algorithm Introduction:

Here we still use an example of the big talk data structure:

Poker is a game that we have played for almost everyone. Usually when we start the time is usually a person licensing, other people are on the side of the card, while the card, if you touch the first card is 5, the second card is 3, naturally we put 3 into the front of 5, the third card is 4, found in the middle of 3 and 5, the last card is 6, put on the back of 5, the fifth card is 2, Plug in front of 3; Finally, when we touch all the cards, the hands of the cards are from small to large (points) ordered.

Let's take a look at this order:

5 3//Insert 3 in an ordered table with only one element 5
3 5 4//Insert 4 in an ordered table with two elements 3 5
3 4 5 6//Insert 6 in an ordered table with two elements 3 4 5
3 4 5 6 2//Insert 2 in an ordered table with two elements 3 4 5 6
2 3 4) 5 6

The basic idea of a direct insert is that each time the first element is taken out of the unordered table, it is inserted into the proper position of the ordered table, so that the ordered table remains orderly.

The first two numbers are compared, then the second number is inserted into the ordered table by size, and the second pass scans the third data with the first two numbers, inserts the third number into the ordered table by size, and then goes through the n-1 scan to complete the sorting process.

A direct insert sort consists of a two-layer nested loop. The outer loop identifies and determines the value to be compared. The inner loop determines its final position for the value to be compared. A direct insert sort compares the value to be compared to its previous value, so the outer loop starts with the second value. The current value continues to loop compared with a value larger than what is to be compared until it is found to be smaller than the value to compare and the value to be compared is placed in a subsequent position, ending the cycle.

The basic method for inserting a sort is to insert a record to be sorted by the size of its keyword into the appropriate position in the previously sorted sequence, each step until all records have been inserted.

Insert Sort Algorithm implementation:

Direct Insert Sort function swap (& $arr, $a, $b) {$temp    = $arr [$a]; $arr [$a] = $arr [$b]; $arr [$b] = $temp;} Function Insertsort (& $arr) {$count = count ($arr); for ($i =1; $i < $count; $i + +) {     $temp = $arr [$i]    ; for ($j = $i-1; $j >= 0 && $arr [$j] > $temp; $j-) {          $arr [$j + 1] = $arr [$j];    Record Move    }    $arr [$j + 1] = $temp;   Insert to the correct position}} $arr = Array (9,1,5,8,3,7,4,6,2); Insertsort ($arr); Var_dump ($arr);

Operation Result:
Array (9) {
[0]=>
Int (1)
[1]=>
Int (2)
[2]=>
Int (3)
[3]=>
Int (4)
[4]=>
Int (5)
[5]=>
Int (6)
[6]=>
Int (7)
[7]=>
Int (8)
[8]=>
Int (9)
}


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.