Summary of some algorithms

Source: Internet
Author: User

//Joseph Ring,use an array to simulate a ring, one per number past, and the end of the array$arr=[1,2,3,4,5,6,7,8,9];$every= 5;//every every time, he kicked out afunctionKick$arr,$every){     while(true){//until the condition of the stop is fulfilled .         for($i= 0;$i<$every-1;$i++){//no need to kick out the number of $every-1            if(Count($arr) {>=1) {//must have at least one, guaranteed to be in accordance with the rules//kick out from the head of the array to the tail                $beshifted=Array_shift($arr); Array_push($arr,$beshifted); }        }        if(Count($arr) {>1) {//More than one, need to kick out            Array_shift($arr); }Else{            return $arr;//only one, then stop        }        }}

<?PHP/*another way of thinking*/$arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];functionKick$arr,$killedNum){     while(Count($arr)){//as long as there are elements, continue to play         for($i= 0;$i<$killedNum-1;$i++){            $shifted=Array_shift($arr);//First kick out            $arr[]=$shifted;//put the end        }        if(Count($arr) {>1) {//Not less than one, then kicked out            Array_shift($arr); }Else{            return $arr;//There's only one, that's the end.        }    }}Print_r(Kick ($arr, 5));

<?PHP/*Bubble sort idea: big cycle, need total number-1 small cycle, first need to check the total number-1 times, gradually minus 1*/$arr=[44,64,634,745,2,6546,1,3334,54];functionBubble$arr){    $compareTime=Count($arr)-1;//how many times in total?    $changeTime=Count($arr)-1;//How many times each comparison is exchanged     for($i= 0;$i<$compareTime;$i++){         for($j= 0;$j<$changeTime;$j++){            if($arr[$j]>$arr[$j+1]){                $tmp=$arr[$j+1]; $arr[$j+1]=$arr[$j]; $arr[$j]=$tmp; }        }        $changeTime--; }    return $arr;}Print_r(Bubble ($arr));

<?PHP//two-dimensional array sorting$person=Array(        Array(' id ' =>2, ' name ' = ' C ', ' age ' =>23),Array(' id ' =>5, ' name ' = ' B ', ' Age ' =>28),Array(' id ' =>3, ' name ' = ' apple ', ' age ' =>17)    );functionArray_sort ($person,$keyValue= ' name '){    $TMPARR=[];//Store the mapping of the corresponding value of the big key and the small key    $FINARR=[];//a two-dimensional array with a good order    foreach($person  as $key=$subarr) {        $TMPARR[$key]=$subarr[$keyValue]; }    Asort($TMPARR);//Asort Sort, and does not change the index relationship    foreach($TMPARR  as $key=$value) {        $FINARR[]=$person[$key]; }    return $FINARR;} $result= Array_sort ($person, ' Age ', 1);Print_r($result);

<?PHP/*an implementation of a fast sort randomly takes a value that is smaller than this value, put together, and then recursively treat the two small values in the same way until the value that is put together can no longer be processed (no or only one)*/$arr=[44,664,3423,5646,112,54555,1,33,3,534534535,1];functionQuicksort$arr){    $relativeKey= Floor(Count($arr)/2);//take out a key, either in the middle or randomly    $relativeNum=$arr[$relativeKey];//This key corresponds to the value    unset($arr[$relativeKey]);//This value is not involved in processing    $left=[];//a value smaller than value    $right=[];//a value larger than value    $sortedLeft=[];//smaller than value, already ordered    $sortedRight=[];//larger than value, ordered    foreach($arr  as $key=$value) {        if($value<=$relativeNum){            $left[]=$value;//Small,}Else{            $right[]=$value;//big.        }    }    if(Count($left) {<=1) {//If there is an empty array on the left or only one, the processing ends        $sortedLeft=$left; }Else{        $sortedLeft=quicksort ($left);//Recursive processing    }    if(Count($right) {<=1) {//right side, left side.        $sortedRight=$right; }Else{        $sortedRight=quicksort ($right); }    return Array_merge($sortedLeft,[$relativeNum],$sortedRight);//the left side of the sequence, the middle, and the right side back together}Print_r(Quicksort ($arr));

Summary of some algorithms

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.