Implementation and efficiency analysis of four sorting algorithms in PHP_php skills

jacklove
Release: 2023-04-01 19:50:02
Original
1510 people have browsed it

This article mainly introduces the implementation and efficiency analysis of PHP's four sorting algorithms. It analyzes the specific definition, usage and algorithm complexity analysis of PHP bubble sort, insertion sort, selection sort and quick sort based on specific examples. It has It must be of reference value. Friends in need can refer to

. This article describes the implementation and efficiency analysis of four sorting algorithms in PHP. Share it with everyone for your reference, the details are as follows:

PHP’s four basic sorting algorithms are: bubble sort, insertion sort, selection sort and quick sort.

The following is the algorithm code I compiled:

1. Bubble sorting:

Idea: perform multiple rounds of bubbles on the array, each In one round, the elements in the array are compared pairwise, the positions are adjusted, and the largest number emerges.

//简单版:
function bubbleSort($arr)
{
   $n = count($arr);
   for($i=1;$i<$n;$i++) { //冒泡的轮数(最多$n-1轮)
     for($j=0;$j<$n-1;$j++) { //每一轮冒泡(两两比较,大者后移)
       if($arr[$j] > $arr[$j+1]) { //前者大于后者,交换位置
          $tmp = $arr[$j];
          $arr[$j] = $arr[$j+1];
          $arr[$j+1] = $tmp;
       }
     }
   }
   return $arr;
}
Copy after login

//改进版:
function bubbleSort($arr)
{
   $n = count($arr);
   for($i=1;$i<$n;$i++) { //冒泡的轮数(最多$n-1轮)
     $flag = 0;  //是否发生位置交换的标志
     for($j=0;$j<$n-$i;$j++) { //每一轮冒泡(两两比较,大者后移)
       if($arr[$j] > $arr[$j+1]) { //前者大于后者,交换位置
          $tmp = $arr[$j];
          $arr[$j] = $arr[$j+1];
          $arr[$j+1] = $tmp;
          $flag = 1;
       }
     }
     if($flag == 0) {  //没有发生位置交换,排序已完成
       break;
     }
   }
   return $arr;
}
Copy after login

In order to improve the efficiency of the bubble sort algorithm, the main areas that need improvement are:

(1 ) Reduce the number of rounds of bubble sorting: When no position exchange occurs in a round of bubble sorting, it means that the array has been sorted and the loop should be exited immediately.

(2) Reduce the number of comparisons in each round: no longer compare some of the elements in the array that have been sorted.

2. Insertion sort:

Idea: Assume that the elements in front of the array are sorted, traverse the elements at the back of the array, and search in the sorted element queue Find a suitable location and insert it.

function insertSort($arr)
{
   $n = count($arr);
   for($i=1;$i<$n;$i++) { //从第二个元素开始插入
     for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
       if($arr[$j] > $arr[$j+1]) { //比前面的数小,交换位置
          $tmp = $arr[$j];
          $arr[$j] = $arr[$j+1];
          $arr[$j+1] = $tmp;
       } else { //大于或等于前面的数,表示已找到插入的位置
          break;
       }
     }
   }
   return $arr;
}
Copy after login

3. Selection sorting:

Idea: Make multiple selections and select the largest element each time Enter the specified location.

function selectSort($arr)
{
   $n = count($arr);
   for($i=$n-1;$i>0;$i--) { //选择排序的轮数($n-1轮)
     $pos = $i; //假设最大元素的位置
     for($j=0;$j<$i;$j++) { //每一轮:从未选择过的元素中选择最大的数
       if($arr[$j] > $arr[$pos]) { //所在位置元素比目前最大元素大,标志其位置
          $pos = $j;
       }
     }
     if($pos != $i) { //将最大元素放入指定的位置
       $tmp = $arr[$pos];
       $arr[$pos] = $arr[$i];
       $arr[$i] = $tmp;
     }
   }
   return $arr;
}
Copy after login

4. Quick sort:

Idea: Recursive algorithm. First select the first element of the array as the standard, then put the numbers less than or equal to it and greater than it into two arrays respectively, perform the same processing on the two arrays, and finally merge the two arrays with the first elements.

function quickSort($arr)
{
   $n = count($arr);
   if($n <= 1) { //若数组只有一个元素,直接返回
     return $arr;
   }
   $largeArr = array(); //存放大数
  $smallArr = array(); //存放小数
   $cur = $arr[0];  //分类基数
   for($i=1;$i<$n;$i++) { //遍历数组元素,对每个元素进行归类
     if($arr[$i] > $cur) {
       $largeArr[] = $arr[$i];
     } else {
       $smallArr[] = $arr[$i];
     }
   }
   //分别对大数组和小数组进行相同的处理
   $smallArr = quickSort($smallArr);
   $largeArr = quickSort($largeArr);
   //合并小数组、分类基数和大数组
   return array_merge($smallArr,array($cur),$largeArr);
}
Copy after login

The time complexity and space complexity of each sorting algorithm:


Sort algorithm Best time analysis Worst time analysis Average time complexity Stable Degree Space complexity
Bubble sort O(n) O(n2) O(n2) Stable O(1)
Insertion sort O(n) O(n2) O(n2) Stable O(1)
Select sort O(n2) O( n2) O(n2) Stable O(1)
Quick Sort O(nlog2n) O(n2) O( nlog2n) Unstable O(log2n)~O(n)

Note: Quick sort is most efficient when the array is out of order, and is least efficient when the array is ordered.

PS: Here is another demonstration tool about sorting recommended for your reference:

Online animation Demonstration of insertion/selection/bubble/merge/Hill/quick sort algorithm process tools:
http://tools.jb51.net/aideddesign/paixu_ys

Articles you may be interested in:

Summary of common methods for obtaining file extensions in PHP

PHP uses Curl to implement simulation Login and data capture function examples php skills

##360 search engine automatically includes php rewrite solution php examples

The above is the detailed content of Implementation and efficiency analysis of four sorting algorithms in PHP_php skills. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template