The algorithm implementation of bubble sort & fast sorting algorithm and binary search & sequential lookup in PHP

Source: Internet
Author: User
This article brings the content is about PHP commonly used in the bubble sort & fast sorting algorithm and binary search & sequential search algorithm implementation, there is a certain reference value, the need for friends can refer to, I hope you have some help.

First, bubble sort

Basic idea:

The arrays that need to be sorted are scanned multiple times from backward (reverse), and the two values are exchanged when the order of the adjacent two values is found to be inconsistent with the rules of the ordering requirement. The smaller (large) values will gradually move from the back to the front.

<?phpfunction Mysort ($arr) {for ($i = 0; $i < count ($arr); $i + +) {$isSort = False;for ($j =0; $j < count ($arr)-$i- 1; $j + +) {if ($arr [$j] < $arr [$j +1]) {$isSort = true; $temp = $arr [$j]; $arr [$j] = $arr [$j +1]; $arr [$j +1] = $temp;}} if ($isSort) {break;}} return $arr;} $arr = Array (3,1,2); Var_dump (Mysort ($arr));? >

Second, quick sort

Basic idea:

Pick an element in the array (the first one) as the ruler, scan through the array to precede the ruler with elements smaller than the ruler, arrange all the elements larger than the ruler after the ruler, and recursively divide each subsequence into smaller sequences until all sequence order is consistent.

<?php//Quick Sort function Quick_sort ($arr) {//First determine if you need to continue $length = count ($arr), if ($length <= 1) {return $arr;} $base _num = $arr [0];//Select a ruler to select the first element//Initialize two arrays $left_array = Array (),///$right_array = array () less than the ruler,//for ($i =1; $i < $length; $i + +) {      //traverse all elements except the ruler, put in two arrays by size relationship if ($base _num > $arr [$i]) {//put in left array $left_array[] = $arr [$i];} else{//put to the right of $ right_array[] = $arr [$i];}} Then the left and right arrays are sorted in the same way//recursive call this function, and record the result $left_array = Quick_sort ($left _array); $right _array = Quick_sort ($right _ array);//merge Left ruler right return Array_merge ($left _array, Array ($base _num), $right _array);} $arr = Array (3,1,2); Var_dump (Quick_sort ($arr));? >

Three or two points find

Basic idea:

Assuming that the data is sorted in ascending order, for a given value x, a comparison is made from the middle of the sequence, if the current position value equals X, the lookup succeeds, and if X is less than the current position value, the first half of the sequence is searched, and if x is greater than the current position, the second half of the series is searched until it is (used when data volume is large)

<?php//binary Find function Bin_search ($arr, $low, $high, $k) {if ($low <= $high) {$mid = Intval (($low + $high)/2); if ($arr [$ MID] = = $k) {return $mid;} else if ($k < $arr [$mid]) {return Bin_search ($arr, $low, $mid-1, $k);} Else{return Bin_search ($arr, $mid +1, $high, $k);}} return-1;} $arr = Array (1,2,3,4,5,6,7,8,9,10);p rint (Bin_search ($arr, 0,9,3));? >

Four, sequential search

Basic idea:

Searches from the first element of the array, one by one, if there are elements consistent with the target, and the lookup succeeds if there is still no target element to the last element, then the lookup fails.

<?php//Order Lookup Function Seq_search ($arr, $n, $k) {$array [$n] = $k; for ($i = 0; $i < $n; $i + +) {if ($arr [$i] = = $k) {break;} if ($i < $n) {return $i;} else{return-1;}}? >

V. Write a function that can traverse all files and subfolders under a file

<?php  function My_scandir ($dir) {$files = array (); if ($handle = Opendir ($dir)) {while ($file = Readdir ($handle))! = = False) {if ($file! = ': ' && $file! = '. ') {if (Is_dir ($dir. ") /". $file)" {$files [$file]=my_scandir ($dir. ") /". $file);} else{$files [] = $file;}}} Closedir ($handle); return $files;}} Var_dump (My_scandir ('.. /'));? >

Six, write a function, as efficiently as possible to remove the file extension from a standard URL

<?phpfunction Getext ($url) {$arr = Parse_url ($url);//parse_url parses a URL and returns an associative array containing the various components that appear in the URL//' scheme ' = String ' http ' (length=4)//' Host ' = + string ' www.sina.com.cn ' (length=15)//' path ' = = String '/abc/de/fg.php ' ( LENGTH=14)//' query ' = = String ' id=1 ' (length=4) $file = basename ($arr [' path ']);//basename function returns the file name portion of the path $ext = Explode ('. ', $file); return $ext [Count ($ext)-1];} Print (Getext (' http://www.sina.com.cn/abc/de/fg.html.php?id=1 '));? >

Seven, the realization of Chinese character string interception without garbled method

You can use MB_SUBSTR, but you need to make sure that Php_mbstring.dll is loaded in the php.ini, that is, to ensure that the "Extension=php_mbstring.dll" line is present and not commented out, otherwise there is an issue with undefined functions.

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.