PHP bubble sorting binary search order query two-dimensional array sorting algorithm function details _ PHP Tutorial

Source: Internet
Author: User
PHP bubble sorting binary search order query two-dimensional array sorting algorithm function details. The data structure is very important. the algorithm + data structure + documentation program uses PHP to describe the bubble sorting algorithm. the object can be an array copy code. the code is as follows: the bubble sorting (array sorting) data structure is very important, algorithm + data structure + document = program
Describes the bubble sorting algorithm using PHP. the object can be an array.

The code is as follows:


// Bubble sort (array sorting)
Function bubble_sort ($ array ){
$ Count = count ($ array );
If ($ count <= 0)
Return false;
For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j -){
If ($ array [$ j] <$ array [$ j-1]) {
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ j-1];
$ Array [$ j-1] = $ tmp;
}
}
}
Return $ array ;}


Using PHP to describe sequential search and binary search (also called semi-query) algorithms, the efficiency of sequential search must be considered. the object can be an ordered array.

The code is as follows:


// Binary search (find an element in the array)
Function bin_sch ($ array, $ low, $ high, $ k ){
If ($ low <= $ high ){
$ Mid = intval ($ low + $ high)/2 );
If ($ array [$ mid] === k ){
Return $ mid;
} Elseif ($ k <$ array [$ mid]) {
Return bin_sch ($ array, $ low, $ mid-1, $ k );
} Else {
Return bin_sch ($ array, $ mid + 1, $ high, $ k );
}
}
Return-1;
}
// Sequential search (find an element in the array)
Function seq_sch ($ array, $ n, $ k ){
$ Array [$ n] = $ k;
For ($ I = 0; $ I <$ n; $ I ++ ){
If ($ array [$ I] === k ){
Break;
}
}
If ($ I <$ n ){
Return $ I;
} Else {
Return-1;
}
}


Write a two-dimensional array sorting algorithm function that is universal and can call php built-in functions

The code is as follows:


// Two-dimensional array sorting. $ arr indicates data, $ keys indicates the key value of sorting, $ order indicates sorting rules, 1 indicates ascending, and 0 indicates descending.
Function array_sort ($ arr, $ keys, $ order = 0 ){
If (! Is_array ($ arr )){
Return false;
}
$ Keysvalue = array ();
Foreach ($ arr as $ key => $ val ){
$ Keysvalue [$ key] = $ val [$ keys];
}
If ($ order = 0 ){
Asort ($ keysvalue );
} Else {
Arsort ($ keysvalue );
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ key => $ vals ){
$ Keysort [$ key] = $ key;
}
$ New_array = array ();
Foreach ($ keysort as $ key => $ val ){
$ New_array [$ key] = $ arr [$ val];
}
Return $ new_array;
}


Pipeline uses PHP to describe the bubble sorting algorithm. the object can be an array code as follows: // bubble sorting (array sorting )...

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.