PHP algorithm----Direct Insert sort

Source: Internet
Author: User

Algorithm Introduction

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.

<?php//using a For loop to insert element 1 into an array [2,3,4,5] head//The idea is to assign the value of the previous array element to the value of the subsequent array element, move the position of all the elements backwards, and then index 0 the position will be empty out of the element that can be inserted into the function Arr_push (Array & $arr, $insert) {$count = count ($arr); for ($i = $count; $i >0; $i-) {$arr [$i]= $arr [$i-1];} $arr [0]= $insert;} $arr = [2,3,4,5];arr_push ($arr, 1), Echo ' <pre> ';p rint_r ($arr);//Direct Insert sort//idea assume that the first element is an ordered table that already exists, From the second element to compare with the elements of the previous ordered table,//The element that is larger than the inserted element is moved backwards, the vacated position is where the element is inserted. Done by the two-layer for loop, the outer control element value, the inner layer as a comparison, and then the back shift function Insertsort (array & $arr) {    $count = count ($arr);    The first element in the array is an ordered table that already exists, so the second element, the element indexed as 1, is compared to the previous ordered table for    ($i = 1; $i < $count; $i + +) {        $temp = $arr [$i];      Set Sentinel, store inserted elements for        ($j = $i-1; $j >= 0 && $arr [$j] > $temp; $j-) {            $arr [$j + 1] = $arr [$j];       Record move, Position empty        }        $arr [$j + 1] = $temp;      The correct position after insertion into the element after the move is empty    } $arr = Array (9,1,5,8,3,7,4,6,2); Insertsort ($arr); Echo ' <pre> ';p rint_r ($arr);       

  

Reference http://blog.csdn.net/baidu_30000217/article/details/53072746

PHP algorithm----Direct Insert sort

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.