Php bubble Sort, quick sort, quick find, two-dimensional array de-weight

Source: Internet
Author: User
This article shares the content of the PHP bubble sort, quick sort, quick find, two-dimensional array de-heavy code, interested friends can take a look

First, bubble sort

Bubble sort function Bubble_sort ($array) {    $count =count ($array);    if ($count <= 0) {        return false;    }    for ($i =0; $i < $count; $i + +) {for        ($j =0; $j < $count-$i-1; $j + +) {            if ($array [$j] > $array [$j +1]) {                $ temp= $array [$j];                $array [$j]= $array [$j +1];                $array [$j +1]= $temp;            }    }} return $array;}

Second, quick sort

Quick-row function Quick_sort ($array) {    $count =count ($array);    if ($count <= 1) {        return $array;    }    $key = $array [0];    $array _left=array ();    $array _right=array ();    for ($i =1; $i < $count; $i + +) {        if ($array [$i] < $key) {            $array _left[]= $array [$i];        } else{            $array _right[]= $array [$i];        }    }    $array _left=quick_sort ($array _left);    $array _right=quick_sort ($array _right);    Return Array_merge ($array _left,array ($key), $array _right);} $myarray =array (1,5,3,4,12,10,8);p Rint_r (Bubble_sort ($myarray)), echo "<br/>";p Rint_r (Quick_sort ($myarray)) ; echo "<br/>";


Third, the position of the first occurrence of the quick Find value

/** * Quick Find value first occurrence position * @param array $array arrays * @param string $k The value to find * @param int $low The minimum key value for the lookup range * @param int $high The maximum key value of the range */function search ($array, $k, $lo       W=0, $high =0) {//determines whether to call if (count ($array)!=0 and $high = = 0) for the first time {$high = count ($array);        }//If there are remaining array elements if ($low <= $high) {//Take $low and $high intermediate values $mid = Intval (($low + $high)/2);        If found, returns the IF ($array [$mid] = = $k) {return $mid;        }//If not found, continue to find ElseIf ($k < $array [$mid]) {return search ($array, $k, $low, $mid-1);        } else {return search ($array, $k, $mid +1, $high); }} return-1;}                  $array = Array (4,5,7,8,9,10,8);                          Test the search function echo search ($array, 8); Call the search function and output the search result 

Four, remove the two-dimensional array duplicates

/** * Remove Duplicates in a two-dimensional array * @param $array array of arrays * @param $keyArray        The key that corresponds to the field at the time of the restore @return array removes the array of duplicates */Public Function ARRAY_UNIQUE_FB ($array, $keyArray) {        $temp =array ();  foreach ($array 2D as $v) {$v = join (",", $v);        Reduced dimension, you can also use implode, a one-dimensional array into a comma-concatenated string $temp [] = $v;    } $temp = Array_unique ($temp);   Remove the duplicated string, which is a repeating one-dimensional array, foreach ($temp as $k + $v) {//$temp [$k] = Explode (",", $v);        Re-assemble the disassembled array $temp [$k]= array_combine ($keyArray, Explode (",", Trim ($v)));    } return $temp; } $testArray =ARRAY_UNIQUE_FB (Array (' A ' =>1, ' B ' =>2, ' C ' =>3), Array (' A ' =>1, ' B ' =>2, ' C ' =>3), Array (' A ' =>1, ' B ' =>2, ' C ' =>3)), Array (' A ', ' B ', ' C '));p Rint_r ($testArray); 

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.