4 kinds of basic sort &2 find

Source: Internet
Author: User

<?php

$arr =array (5,2,1,7,8,6,3,10,9);

Showarray ($arr);

/**

* Traverse Print Array

* @param string $type sorting method

* @param array $arr sorted

*/

function Showarray ($arr, $type = ' original array ') {

echo "$type:<br/>";

foreach ($arr as $v) {

Echo '. $v. ';

}

Echo ' <br/> ';

}



/**

* Bubble Sort

* From small to large, the large boundary gradually drops

* @param array $arr

*/

function Bubblesort ($arr) {

for ($i =0; $i <count ($arr)-1; $i + +) {

for ($j = 0; $j < count ($arr)-$i; $j + +) {

if ($arr [$j]> $arr [$j +1]) {

$temp = $arr [$j +1];

$arr [$j +1]= $arr [$j];

$arr [$j]= $temp;

}

}

}

return $arr;

}

Showarray (Bubblesort ($arr), ' bubble sort ');


/**

* Select sort

* Find the minimum value, the small boundary gradually to large contraction

* @param array $arr

*/

function Selectsort ($arr) {

for ($i = 0; $i < count ($arr); $i + +) {

$minVal = $arr [$i];

$minIndex = $i;

for ($j = 0+1+ $i; $j < count ($arr); $j + +) {//self-shrinking with less than +1 small borders + $i

if ($minVal > $arr [$j]) {//Find the minimum value, in order to compare the last

$minVal = $arr [$j];

$minIndex = $j;

}

}

$temp = $arr [$i];

$arr [$i]= $minVal;

$arr [$minIndex]= $temp;

}

return $arr;

}

Showarray (Selectsort ($arr), ' select Sort ');


/**

* Insert Sort

* No border control, with mini left offset contrast, reached on the right of the array to move the slot, insert

* @param array $arr

*/

function Insertsort ($arr) {

for ($i = 1; $i < count ($arr), $i + +) {//starting from 1, vs. 1-bit

$minV = $arr [$i];

$minI = $i-1;

while ($minI >=0 && $minV < $arr [$minI]) {//left-to-right $MINV position

$arr [$minI +1]= $arr [$minI];

$minI--;

}

$arr [$minI +1]= $minV;

}

return $arr;

}

Showarray (Insertsort ($arr), ' Insert sort ');

/**

* Quick Sort

* @param unknown $arr

* @return Unknown|multitype:

*/

function QuickSort ($arr) {

if (count ($arr) <=1) return $arr;

$flag = $arr [0];

$left =array ();

$right =array ();

for ($i = 1; $i < count ($arr); $i + +) {

if ($arr [$i]> $flag) {

$left []= $arr [$i];

}else {

$right []= $arr [$i];

}

}//Divide the size once, then divide

$left =quicksort ($left);

$right =quicksort ($right);

Return Array_merge ($right, Array ($flag), $left);

}

Showarray (QuickSort ($arr), ' quick sort ');


/**

* Binary Method Search

* @param unknown $a

* @param unknown $val

* @return Number

*/

function BinarySearch ($a, $val) {

$low = 0;

$high = count ($a)-1;

while ($low <= $high) {

$mid = Intval (($low + $high)/2);

if ($a [$mid] = = $val)

return $mid;

if ($a [$mid] > $val) {

$high = $mid-1;

} else {

$low = $mid + 1;

}

}

return-1;

}

$a =quicksort ($arr);

Echo BinarySearch ($a, 7);

/**

* Sequential Search

*/

Function Search (& $arr, $findVal) {

$flag =false;

for ($i =0; $i <count ($arr); $i + +) {

if ($findVal = = $arr [$i]) {

echo "found, subscript = $i";

$flag =true;

Break

}

}

if (! $flag) {

echo ' Query not ';

}

}

Search ($arr,-1);


This article from "The beginning of the more" blog, declined reprint!

4 kinds of basic sort &2 find

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.