Php bubble sorting, quick sorting, quick search, and two-dimensional array deduplication _ PHP Tutorial

Source: Internet
Author: User
Php bubble sorting, quick sorting, quick search, and two-dimensional array deduplication instance sharing. I. Code of the bubble sort copy code: functionbubble_sort ($ array) {$ countcount ($ array); if ($ count0) {returnfalse;} for ($ i0; $ I $ count; $ I ++) {for ($ j0; 1. Bubble sorting

The code is as follows:


// 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;
}

II. quick sorting

The code is as follows:


// Fast sorting
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 (, 10, 8 );
Print_r (bubble_sort ($ myarray ));
Echo"
";
Print_r (quick_sort ($ myarray ));
Echo"
";

3. quick search for the position where the value appears for the first time

The code is as follows:

/**
* Quickly find the location where the value appears for the first time
* @ Param array $ array
* @ Param string $ k value
* @ Param int $ minimum key value of the low search range
* @ Param int $ maximum key value in the high range
*/
Function search ($ array, $ k, $ low = 0, $ high = 0)
{
// Determine whether it is the first call
If (count ($ array )! = 0 and $ high = 0 ){
$ High = count ($ array );
}
// If there are still remaining array elements
If ($ low <= $ high ){
// Take the center values of $ low and $ high
$ Mid = intval ($ low + $ high)/2 );
// If it is found, return
If ($ array [$ mid] === k ){
Return $ mid;
}
// If not found, continue searching
Elseif ($ k <$ array [$ mid]) {
Return search ($ array, $ k, $ low, $ mid-1 );
}
Else {
Return search ($ array, $ k, $ mid + 1, $ high );
}
}
Return-1;
}
$ Array = array (, 8); // test the search function
Echo search ($ array, 8); // call the search function and output the search result.

4. remove two-dimensional array duplicates

The code is as follows:

/**
* Remove repeated items from the two-dimensional array
* @ Param $ array2D array
* @ Param $ the key corresponding to the field when the keyArray is restored
* @ Return array removes the array of repeated items
*/
Public function array_unique_fb ($ array2D, $ keyArray ){
$ Temp = array ();
Foreach ($ array2D as $ v ){
$ V = join (",", $ v); // dimension reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,).
$ Temp [] = $ v;
}
$ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
Foreach ($ temp as $ k => $ v ){
// $ Temp [$ k] = explode (",", $ v); // re-assemble the split 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 ''));

Print_r ($ testArray );

Example code: // bubble sort function bubble_sort ($ array) {$ count = count ($ array); if ($ count = 0) {return false ;} for ($ I = 0; $ I $ count; $ I ++) {for ($ j = 0 ;...

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.