Several common sorts of PHP and two-way search

Source: Internet
Author: User

<?php

1. Insert Sort

Thought:

Each time a data element to be sorted is inserted into a sequential sequence, the sequence remains orderly, knowing that the data elements to be sorted are all inserted.

Example:

[Initial key Words] [49] 38 65 97 76 13 27 49
j=2 (38) [38 49] 65 97 76 13 27 49
J=3 (65) [38 49 65] 97 76 13 27 49
J=4 (97) [38 49 65 97] 76 13 27 49
J=5 (76) [38 49 65 76 97] 13 27 49
J=6 (13) [13 38 49 65 76 97] 27 49
J=7 (27) [13 27 38 49 65 76 97] 49
J=8 (49) [13 27 38 49 49 65 76 97]

Complexity of Time:

If the goal is to arrange the sequence of n elements in ascending order, then the insertion sort is the best and worst case scenario. The best case is that the sequence is already in ascending order, in which case the comparison operation needs to be done (n-1). The worst case scenario is that the sequence is sorted in descending order, then there is a total of n (n-1)/2 times to be performed at this point. The assignment operation to insert a sort is the number of times the comparison operation is added (n-1). On average, the time complexity of inserting the sorting algorithm is O (n^2). Thus, the insertion sort is not suitable for applications with a large amount of data for sorting. However, if the amount of data that needs to be sorted is small, for example, if the magnitude is less than thousand, then inserting the sort is a good choice.


Insert Sort
function Insertsort ($arr) {
$count =count ($arr);
for ($i =1; $i < $count; $i + +) {
$tem = $arr [$i];
$j = $i-1;
while ($arr [$j]> $tem) {
$arr [$j +1]= $arr [$j];
$arr [$j]= $tem;
$j--;
}
}
return $arr;

}

2. Select Sort

thought : Each trip selects the smallest (or largest) element from the data element to be sorted, placed in the final order of the ordered sequence, until all the data elements are sorted out.

Example:

[Initial key Words] [49 38 65 97 76 13 27 49]
First trip sorted after 13 [38 65 97 76 49 27 49]
Second trip sorted after 13 27 [65 97 76 49 38 49]
Third trip sort after 13 27 38 [97 76 49 65 49]
Four-trip sort after 13 27 38 49 [49 97 65 76]
Five-trip sort after 13 27 38 49 49 [97 97 76]
Six-trip sort after 13 27 38 49 49 76 [76 97]
Seventh trip sort after 13 27 38 49 49 76 76 [97]
Last sorted results 13 27 38 49 49 76 76 97

Complexity of Time:

Time complexity O (N2), unstable sequencing, suitable for smaller scale


Select sort
function Selectsort ($arr) {
$count = count ($arr);
for ($i =0; $i < $count-1; $i + +) {
$k = $i;
for ($j = $i +1; $j < $count; $j + +) {
if ($arr [$k]> $arr [$j])
$k = $j;
}
$temp = $arr [$i];
$arr [$i]= $arr [$k];
$arr [$k]= $temp;
}
return $arr;
}

3. Bubble sort

Thought:

22 compares the size of the data element to be sorted, and finds that two data elements are exchanged in reverse order until there are no reversed data elements.

Example:

49 13 13 13 13 13 13 13
38 49 27 27 27 27 27 27
65 38 49 38 38 38 38 38
97 65 38 49 49 49 49 49
76 97 65 49 49 49 49 49
13 76 97 65 65 65 65 65
27 27 76 97 76 76 76 76
49 49 49 76 97 97 97 97

Complexity of Time:

The time complexity of the algorithm is O (N2). However, when the original keyword sequence is ordered, only one trip to the comparison ends, at which time the complexity is O (n).


Bubble sort
function Bubblesort ($arr) {
$count = count ($arr);
if ($count <= 0) return false;
for ($i =0; $i <count ($arr); $i + +) {
$flag = false; Before this sequencing begins, the swap flag should be false
For ($j =count ($arr)-1; $j > $i; $j-) {
if ($arr [$j -1]> $arr [$j]) {
$temp = $arr [$j-1];
$arr [$j -1]= $arr [$j];
$arr [$j]= $temp;
$flag =true;
}
}
if (! $flag)//The current order does not occur interchange, early termination algorithm
return $arr;
}
return $arr;
}

4. Quick Sort

Thought:

By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Complexity of Time:

The fast-sorting body algorithm has a time operation of approximately O (log2n), and the calculated amount of the molecular area function is approximately o (n), so the total time complexity is O (nlog2n), which is obviously better than the Bubble sort O (N2). But the advantage of the algorithm is not absolute. Test analysis, when the original file keyword ordered, fast sorting time complexity is O (N2), in this case fast sorting unpleasant. And the bubble sort of this situation is O (n), but soon. Fast sorting is considered to be the best sort of sorting method in which the original file records the keyword unordered.


Quick Sort
function QuickSort ($arr) {
if (count ($arr) <=1)
return $arr;
$key = $arr [0];
$left _arr=array ();
$right _arr=array ();
for ($i =1; $i <count ($arr); $i + +) {
if ($arr [$i]<= $key) {
$left _arr[]= $arr [$i];
}else{
$right _arr[]= $arr [$i];
}
}
$left _arr=quicksort ($left _arr);
$right _arr=quicksort ($right _arr);
Return Array_merge ($left _arr,array ($key), $right _arr);
}


$arr =array (49,38,65,97,76,13,27,49);
Print_r (QuickSort ($arr));






Binary Method Search
function BinarySearch ($arr, $value, $start = 0, $end = NULL) {
if ($end = = NULL) $end = count ($arr)-1;
$index = Floor (($start + $end)/2);
$base = $arr [$index];
if ($value < $base) return BinarySearch ($arr, $value, $start, $index-1);
else if ($value > $base) return BinarySearch ($arr, $value, $index +1, $end);
else return $index;
}


$arr = Array (1, 3, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20);
$value = 8;
Echo BinarySearch ($arr, $value);

Several common sorts of PHP and two-way search

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.