A sort algorithm in PHP implementation data structure

Source: Internet
Author: User
Tags array length sort

Bubble sort

Principle

The adjacent two numbers are compared in turn, the decimal places are in front, the large number is in the back. At the end of the first trip, put the biggest number to the end. At the end of the second trip, put the biggest number to the bottom of the second. All the way down until the final sort is finished.

Bubble sort, only need to use two-loop implementation, time complexity of O (n*n).

"Code Implementation"

Implementation: 22 comparison, put the small number in the front

function Bubble_sort ($array)

{

if (!is_array ($array))

{

return false;

}

$len =count ($array);

For ($i =0 $i < $len-1; $i + +)

{

$flag = false;

For ($j =0 $j < $len-$i-1; $j + +)

{

if ($array [$j] > $array [$j +1])

{

$temp = $array [$j];

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

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

$flag = true;

}

}

if (! $flag)//This trip sort has no value exchange, then terminate the program in advance

return $array;

}

}

Call implementation

$array = Array (7, 2, 4, 1, 6, 3, 8, 0, 5);

Print_r (Bubble_sort ($array));

?>

Hill sort

Principle

First, take an integer d1 less than n as the first increment, dividing the entire record of the file into D1 groups. All distances for D1

is placed in the same group as the multiple records. First, the direct insertion sort within each group; Then, take the second increment D2

The above grouping and sorting until the increment dt=1 is taken (dt

Insert the sort directly.

"Code Implementation"

Implementation: Compare the number of incremental intervals until the large number is put to the last

/**

* Data structure in Hill sort PHP implementation

*

* Time complexity of O (n (logn) 2)

*/

function Shell_sort ($array)

{

if (!is_array ($array))

{

return false;

}

$len =count ($array);

$d = $len;//random increment with an initial value of array length to continuously remove 2 values

while ($d >1)

{

$d =intval ($d/2);//group interval, 2 is n value, n value reduced, mobile number of trips and data increased

$temp =null;

$j = 0;

for ($i = $d; $i < $len; $i = = $d)

{

if ($array [$i] < $array [$i-$d])

{

$temp = $array [$i];

$j = $i-$d;

while (($j >=0) && $temp < $array [$j])

{

$array [$j + $d]= $array [$j];

$j = $j-$d;

}

$array [$j + $d]= $temp;

}

}

}

return $array;

}

$array = Array (7, 2, 4, 1, 6, 3, 8, 0, 5);

Print_r (Shell_sort ($array));

?>

Select sort

Principle

Each trip selects the smallest (or largest) element from the data element to be sorted, sequentially at the end of the ordered sequence, until all the data elements to be sorted are finished. Selecting a sort is an unstable sort method.

"Code Implementation"

Implementation: Two cycles, all compared to find the smallest element of the key (the index in the array) to determine the current number and the minimum key, unequal is the exchange value

/**

* Data structure to select the sort of PHP implementation

*

* Time complexity of O (N2)

*/

function Select_sort ($array)

{

if (!is_array ($array))

{

return false;

}

$len =count ($array);

for ($i =0; $i < $len; $i + +)

{

$k = $i;

for ($j = $i +1; $j < $len; $j + +)

{

if ($array [$k] > $array [$j])

{

$k = $j;

}

}

if ($i!= $k) {

$temp = $array [$i];

$array [$i] = $array [$k];

$array [$k] = $temp;

}

}

return $array;

}

$array = Array (7, 2, 4, 1, 6, 3, 8, 0, 5);

Print_r (Select_sort ($array));

?>

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.