Sorting algorithm :

(1) Bubble sort

 $arr = array(15,8,20,50,37,85,10,5,11,4);
// Bubble sort
function maoPao($arr){
for($i = 0; $i < count($arr)-1; $i++){
for($j = 0; $j < count($arr)-1; $j++){
if($arr[$j] > $arr[$j+1]){
$temp = $arr[$j];
$arr[$j] = $arr[$j+1];
$arr[$j+1] = $temp;
}
}
}
return $arr;
}

(2) Quick sort

 $arr = array(15,8,20,50,37,85,10,5,11,4);
// Quick sort
function fastSort($arr){
$len = count($arr); if($len <= 1){
return $arr;
} $key = $arr[0];
$left = array();
$right = array(); for($i = 1; $i < $len; $i++){
if($arr[$i] > $key){
$right[] = $arr[$i];
} else {
$left[] = $arr[$i];
}
} $left = fastSort($left);
$right = fastSort($right); return array_merge($left, array($key), $right);
}

Search algorithm :

(1): Ordinary ( For irregular arrays , But the efficiency is relatively low )

 function search($arr, $val){
foreach($arr as $k => $v){
if($v == $val){
return $k;
}
}
return -1;
}

(2): Two points search ( Only for ordered index arrays , More efficient )

 // Dichotomy search 
function binSearch($arr, $val){
$len = count($arr);
$low = 0;
$end = $len - 1; while($low <= $end){
$mid = floor(($low+$end)/2); if($arr[$mid] == $val){
return $mid;
} else if($arr[$mid] > $val){
$end = $mid - 1;
} else {
$low = $mid + 1;
}
} return -1;
}

PHP The sorting algorithm and search algorithm of more related articles

  1. cb34a_c++_STL_ Algorithm _ Search algorithm _(7)_lower_bound

    cb34a_c++_STL_ Algorithm _ Search algorithm _(7)_lower_bound// Search algorithm for ordered intervals , Such as set,multiset Associate container - Automatic sorting lower_bound()-- The first possible location uppe ...

  2. cb33a_c++_STL_ Algorithm _ Search algorithm _(6)binary_search_includes

    cb33a_c++_STL_ Algorithm _ Search algorithm _(6)binary_search_includes// Search algorithm for ordered intervals , Such as set,multiset Associate container - Automatic sorting binary_search(b,e ...

  3. cb32a_c++_STL_ Algorithm _ Search algorithm _(5)adjacent_find

    cb32a_c++_STL_ Algorithm _ Search algorithm _(5)adjacent_findadjacent_find(b,e),b,begin(),e,end()adjacent_find(b,e,p),p-par ...

  4. cb28a_c++_STL_ Algorithm _ Search algorithm _(1)find_find_if

    cb28a_c++_STL_ Algorithm _ Search algorithm _(1)find_find_iffind() // Linear search , It's slow .pos1 = find(ilist.begin(), ilist.end(), 5);fi ...

  5. cb31a_c++_STL_ Algorithm _ Search algorithm _(4)find_first_of

    cb31a_c++_STL_ Algorithm _ Search algorithm _(4)find_first_offind_first_of(b,e,sb,se),sb,second begin, se,second end();find ...

  6. cb30a_c++_STL_ Algorithm _ Search algorithm _(3)search_find_end

    cb30a_c++_STL_ Algorithm _ Search algorithm _(3)search_find_endsearch()pos = search(ideq.begin(), ideq.end(), ilist.begin() ...

  7. cb29a_c++_STL_ Algorithm _ Search algorithm _(2)search_n

    cb29a_c++_STL_ Algorithm _ Search algorithm _(2)search_n// such as : Continuous search continuous n individual 8search_n(b,e,c,v), iterator b,begin(),e,end(). Successive c individual vpos=sea ...

  8. PHP Array basic sorting algorithm and search algorithm

    About PHP The basic algorithm in , To sum up , It's also the first article of this blog 1.2 A sort algorithm bubble sort : Example : Personal opinion 5 6 2 3 7 9 The first trip 5 6 2 3 7 9 5 2 6 3 7 9 5 2 3 6 7 ...

  9. STL_ Algorithm _01_ Search algorithm

    1. From the tutorial : The first 6 speak PPT.15 ◆  Common search algorithms : 1.1. Search by condition N Two adjacent elements ( adjacent yes Proximity means ) iterator = adjacent_find(iterator ...

Random recommendation

  1. winform How to realize the pop-up effect in the lower right corner

    [DllImport("user32")] private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, in ...

  2. android Send SMS function

    private void sendSMS(String num,String smsBody) { String phoneNum = "smsto:" + num; Uri sm ...

  3. LYDSY Simulation game day2 Market

    /* orz claris, The solution to this problem is very clever , First, it's a matter of time , In fact, this problem only needs to be dealt with offline , Sort things and inquiries in chronological order , And see if you can satisfy . then , Because of the capacity <=10^9, It's obviously impossible ...

  4. c++ The container of learning, the details (1)

    In contrast to c++primier Start learning Chapter 9 containers , Take the exercises after class as exercises , Although I read the explanation in the book , But when programming , There are still some omissions in some areas that need attention . Here are some impressive summaries : (1) The first few chapters are just ve ...

  5. Muzei Live Wallpaper Wallpaper application Android source project

    This was just seen on Android tutorial website , This is Muzei Live Wallpaper Wallpaper application source code , This is already in Playstore Online android Wallpaper applications , Click on the wallpaper interface to smooth between the transparent effect and the general background effect ...

  6. MySQL Of 20 Basic optimization Plus references

    mysql The performance optimization of is very extensive : Index optimization , Query optimization , The query cache , Server setup optimization , Operating system and hardware optimization , Application level optimization (web The server , cache ) wait . The documented optimization techniques here are more suitable for developers , They are all collected from the Internet and sorted out by themselves ...

  7. Linux command - lsof: Lists open files

    lsof  Is a list of the current system open file tools . Command format lsof  [  -?abChlnNOPRstUvVX  ] [ -A A ] [ -c c ] [ +c c ] [ +|-d d ] [ + ...

  8. Google Maps API v2 Demo Tutorial

    apply API KEY https://code.google.com/apis/console/?noredirect 1. Create project , Name at will , Just to distinguish 2. Turn on Google Maps Andr ...

  9. linuxc_ Thread locking pthread_mutex_t

    Threads in actual execution , We often need to synchronize multiple threads . Then you can use mutexes to accomplish the task : Mutex in use , Yes pthread_mutex_init,pthread_mutex_destory,pthread ...

  10. JavaBean Entry notes

    I saw JavaBean I feel confused , Do not know what that mean? , Until I checked the data and found that I had misunderstood , hold JavaBean Mistaking it for a technology , Actually Java Bean Just in line with certain norms Java class , Easy to encapsulate and reuse . In line with this standard Jav ...